Skip to content

Commit

Permalink
Merge pull request #2 from OpenCHAMI/alt-code-review
Browse files Browse the repository at this point in the history
Alt code review
  • Loading branch information
travisbcotton committed Mar 6, 2024
2 parents 37ddc07 + b27898b commit 74c3066
Show file tree
Hide file tree
Showing 13 changed files with 351 additions and 181 deletions.
41 changes: 41 additions & 0 deletions .github/workflows/ochami.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# This workflow will build and release a golang-based microservice
# using goreleaser any time a new version tag is pushed to the repository

name: Release with goreleaser

on:
push:
tags:
- v*
permissions: write-all # Necessary for creating containers

jobs:

build:
runs-on: ubuntu-latest

steps:
- name: Set up Go 1.21
uses: actions/setup-go@v5
with:
go-version: 1.21
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Docker Login
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Checkout
uses: actions/checkout@v4
with:
fetch-tags: 1
fetch-depth: 1
- name: Release with goreleaser
uses: goreleaser/goreleaser-action@v5
env:
GITHUB_TOKEN: ${{ github.token }}
with:
version: latest
args: release --clean
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist
1 change: 0 additions & 1 deletion .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ dockers:
- LICENSE
- CHANGELOG.md
- README.md
- .version

archives:
- format: tar.gz
Expand Down
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.0.4] - 2024-01-17

### Added

- Initial release
- Created SMD client
- Added memory-based store
- Able to provide cloud-init payloads that work with newly booted nodes
- Build and release with goreleaser
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ RUN set -ex \
&& apk add --no-cache curl

# Get the boot-script-service from the builder stage.
COPY cloud-init-service /usr/local/bin/
COPY .version /
COPY cloud-init-server /usr/local/bin/


# nobody 65534:65534
USER 65534:65534

# Set up the command to start the service.
CMD /usr/local/bin/cloud-init-service \
CMD /usr/local/bin/cloud-init-server \
--ci-listen ":27777" \
--smd-endpoint "http://localhost:27779"

Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2024 OpenCHAMI
Copyright © 2024 Triad National Security, LLC. This program was produced under U.S. Government contract 89233218CNA000001 for Los Alamos National Laboratory (LANL), which is operated by Triad National Security, LLC for the U.S. Department of Energy/National Nuclear Security Administration.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# harbor
# Cloud-Init

Microservice that servers out cloud-init configs
OpenCHAMI cloud-init server that retrieves detailed inventory information from SMD and uses it to create cloud-init payloads customized for each node in an ochami-based cluster
167 changes: 167 additions & 0 deletions cmd/cloud-init-server/handlers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
package main

import (
"fmt"
"net/http"

"github.com/OpenCHAMI/cloud-init/internal/memstore"
"github.com/OpenCHAMI/cloud-init/internal/smdclient"
"github.com/OpenCHAMI/cloud-init/pkg/citypes"
"github.com/gin-gonic/gin"
"github.com/gosimple/slug"
yaml "gopkg.in/yaml.v2"
)

type CiHandler struct {
store ciStore
sm *smdclient.SMDClient
}

func NewCiHandler(s ciStore, c *smdclient.SMDClient) *CiHandler {
return &CiHandler{
store: s,
sm: c,
}
}

// ListEntries godoc
// @Summary List all cloud-init entries
// @Description List all cloud-init entries
// @Produce json
// @Success 200 {object} map[string]CI
// @Router /harbor [get]
func (h CiHandler) ListEntries(c *gin.Context) {
ci, err := h.store.List()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
}

c.JSON(200, ci)
}

// AddEntry godoc
// @Summary Add a new cloud-init entry
// @Description Add a new cloud-init entry
// @Accept json
// @Produce json
// @Param ci body CI true "Cloud-init entry to add"
// @Success 200 {string} string "name of the new entry"
// @Failure 400 {string} string "bad request"
// @Failure 500 {string} string "internal server error"
// @Router /harbor [post]
func (h CiHandler) AddEntry(c *gin.Context) {
var ci citypes.CI
if err := c.ShouldBindJSON(&ci); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

id := slug.Make(ci.Name)

err := h.store.Add(id, ci)
if err != nil {
if err == memstore.ExistingEntryErr {
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}

c.JSON(http.StatusOK, ci.Name)
}

// GetEntry godoc
// @Summary Get a cloud-init entry
// @Description Get a cloud-init entry
// @Produce json
// @Param id path string true "ID of the cloud-init entry to get"
// @Success 200 {object} CI
// @Failure 404 {string} string "not found"
// @Router /harbor/{id} [get]
func (h CiHandler) GetEntry(c *gin.Context) {
id := c.Param("id")

ci, err := h.store.Get(id, h.sm)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
}

c.JSON(200, ci)
}

func (h CiHandler) GetUserData(c *gin.Context) {
id := c.Param("id")

ci, err := h.store.Get(id, h.sm)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
}
ud, err := yaml.Marshal(ci.CIData.UserData)
if err != nil {
fmt.Print(err)
}
s := fmt.Sprintf("#cloud-config\n%s", string(ud[:]))
//c.Header("Content-Type", "text/yaml")
c.Data(200, "text/yaml", []byte(s))
}

func (h CiHandler) GetMetaData(c *gin.Context) {
id := c.Param("id")

ci, err := h.store.Get(id, h.sm)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
}

c.YAML(200, ci.CIData.MetaData)
}

func (h CiHandler) GetVendorData(c *gin.Context) {
id := c.Param("id")

ci, err := h.store.Get(id, h.sm)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
}

c.YAML(200, ci.CIData.VendorData)
}

func (h CiHandler) UpdateEntry(c *gin.Context) {
var ci citypes.CI
if err := c.ShouldBindJSON(&ci); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

id := c.Param("id")

err := h.store.Update(id, ci)
if err != nil {
if err == memstore.NotFoundErr {
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}

c.JSON(http.StatusOK, id)
}

func (h CiHandler) DeleteEntry(c *gin.Context) {
id := c.Param("id")

err := h.store.Remove(id)
if err != nil {
if err == memstore.NotFoundErr {
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}

c.JSON(http.StatusOK, gin.H{"status": "success"})
}
Loading

0 comments on commit 74c3066

Please sign in to comment.