Skip to content

Commit

Permalink
patch: removed server_flavor_id as an input option. Generated from op…
Browse files Browse the repository at this point in the history
…enstack config
  • Loading branch information
drew-viles committed Oct 28, 2022
1 parent 46f0923 commit cc595de
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 20 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ on:
push:
tags:
- "v*.*.*"
# workflow_dispatch:
# schedule:
# - cron: '40 19 * * *' # once a day at 19:40 UTC / 11:40 PST
workflow_dispatch:

jobs:
lint:
Expand Down
31 changes: 25 additions & 6 deletions cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ package cmd

import (
"bufio"
"github.com/drew-viles/baskio/pkg/file"
"encoding/json"
gitRepo "github.com/drew-viles/baskio/pkg/git"
ostack "github.com/drew-viles/baskio/pkg/openstack"
systemUtils "github.com/drew-viles/baskio/pkg/system"
"github.com/go-git/go-git/v5/plumbing"
"github.com/google/uuid"
Expand Down Expand Up @@ -54,16 +55,34 @@ func fetchBuildRepo(imageRepo string) string {
return g
}

//// copyVariablesFile copies the contents of the provided file into the correct location
//// so the that build can be performed.
//func copyVariablesFile(buildGitDir string, buildOS, configPath string) {
// log.Printf("copying variables file\n")
// outputFileName := strings.Join([]string{"openstack-", buildOS, ".json"}, "")
// outputFile := filepath.Join(buildGitDir, "images/capi/packer/openstack", outputFileName)
//
// _, err := file.CopyFile(configPath, outputFile)
// if err != nil {
// panic(err)
// }
//}

// copyVariablesFile copies the contents of the provided file into the correct location
// so the that build can be performed.
func copyVariablesFile(buildGitDir string, buildOS, configPath string) {
log.Printf("copying variables file\n")
outputFileName := strings.Join([]string{"openstack-", buildOS, ".json"}, "")
func generateVariablesFile(buildGitDir string, buildConfig *ostack.BuildConfig) {
log.Printf("generating variables file\n")
outputFileName := strings.Join([]string{"openstack-", buildConfig.BuildName, ".json"}, "")
outputFile := filepath.Join(buildGitDir, "images/capi/packer/openstack", outputFileName)

_, err := file.CopyFile(configPath, outputFile)
configContent, err := json.Marshal(buildConfig)
if err != nil {
panic(err)
log.Fatalln(err)
}

err = os.WriteFile(outputFile, configContent, 0644)
if err != nil {
log.Fatalln(err)
}
}

Expand Down
14 changes: 8 additions & 6 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ var (
osAuthPluginFlag string

//Additional Openstack flags
networkIDFlag, serverFlavorIDFlag, openstackBuildConfigPathFlag string
enableConfigDriveFlag bool
networkIDFlag, openstackBuildConfigPathFlag string
enableConfigDriveFlag bool

//Build flags
repoRoot = "https://github.com/eschercloudai/image-builder"
Expand Down Expand Up @@ -67,7 +67,6 @@ func init() {
IdentityAPIVersion: osIdentityAPIVersionFlag,
AuthPlugin: osAuthPluginFlag,
NetworkID: networkIDFlag,
ServerFlavorID: serverFlavorIDFlag,
OpenstackBuildConfigPath: openstackBuildConfigPathFlag,
EnableConfigDrive: fmt.Sprintf("%t", enableConfigDriveFlag),
ImageRepo: imageRepoFlag,
Expand All @@ -87,7 +86,11 @@ func init() {

//Build image
buildGitDir := fetchBuildRepo(envs.ImageRepo)
copyVariablesFile(buildGitDir, envs.BuildOS, envs.OpenstackBuildConfigPath)
buildConfig := ostack.ParseBuildConfig(envs.OpenstackBuildConfigPath)
buildConfig.Networks = envs.NetworkID

generateVariablesFile(buildGitDir, buildConfig)

capiPath := filepath.Join(buildGitDir, "images/capi")
fetchDependencies(capiPath)
err := buildImage(capiPath, envs.BuildOS)
Expand All @@ -102,7 +105,7 @@ func init() {
//Scan image
osClient.OpenstackInit()
kp := osClient.CreateKeypair()
server, freeIP := osClient.CreateServer(kp, imgID, serverFlavorIDFlag, networkIDFlag, enableConfigDriveFlag)
server, freeIP := osClient.CreateServer(kp, imgID, buildConfig.Flavor, buildConfig.Networks, enableConfigDriveFlag)

resultsFile, err := fetchResultsFromServer(freeIP, kp)
if err != nil {
Expand Down Expand Up @@ -154,7 +157,6 @@ func init() {
rootCmd.Flags().StringVar(&osAuthPluginFlag, "os-auth-plugin", "password", "The Openstack Auth Plugin (Can also set env OS_AUTH_PLUGIN)")
rootCmd.Flags().BoolVarP(&enableConfigDriveFlag, "enable-config-drive", "c", false, "Used to enable a config drive on Openstack. This may be required if using an external network. (Can also set env OS_ENABLE_CONFIG_DRIVE)")
rootCmd.Flags().StringVarP(&networkIDFlag, "network-id", "n", "", "Network ID to deploy the server onto for scanning. (Can also set env OS_NETWORK_ID)")
rootCmd.Flags().StringVarP(&serverFlavorIDFlag, "server-flavor-id", "s", "", "ID of the server flavor to create for the scan. (Can also set env OS_SERVER_FLAVOR_ID)")

//Configuration items
rootCmd.Flags().StringVarP(&openstackBuildConfigPathFlag, "build-config", "b", "", strings.Join([]string{"The openstack variables to use to build the image (see ", repoRoot, "/blob/master/images/capi/packer/openstack/openstack-ubuntu-2004.json) (Can also set env OS_BUILD_CONFIG)"}, ""))
Expand Down
4 changes: 0 additions & 4 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ type Env struct {
IdentityAPIVersion string
AuthPlugin string
NetworkID string
ServerFlavorID string
OpenstackBuildConfigPath string
EnableConfigDrive string
ImageRepo string
Expand Down Expand Up @@ -222,9 +221,6 @@ func (e *Env) CheckForEnvVars() {
if !checkEnv(e, "NetworkID", "OS_NETWORK_ID") {
canContinue = false
}
if !checkEnv(e, "ServerFlavorID", "OS_SERVER_FLAVOR_ID") {
canContinue = false
}
if !checkEnv(e, "EnableConfigDrive", "OS_ENABLE_CONFIG_DRIVE") {
canContinue = false
}
Expand Down
44 changes: 44 additions & 0 deletions pkg/openstack/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package ostack

import (
"encoding/json"
"log"
"os"
)

type BuildConfig struct {
BuildName string `json:"build_name"`
DistroName string `json:"distro_name"`
GuestOsType string `json:"guest_os_type"`
OsDisplayName string `json:"os_display_name"`
ShutdownCommand string `json:"shutdown_command"`
SshUsername string `json:"ssh_username"`
SourceImage string `json:"source_image"`
Networks string `json:"networks"`
Flavor string `json:"flavor"`
AttachConfigDrive string `json:"attach_config_drive"`
UseFloatingIp string `json:"use_floating_ip"`
FloatingIpNetwork string `json:"floating_ip_network"`
CrictlVersion string `json:"crictl_version"`
KubernetesSemver string `json:"kubernetes_semver"`
KubernetesRpmVersion string `json:"kubernetes_rpm_version"`
KubernetesSeries string `json:"kubernetes_series"`
KubernetesDebVersion string `json:"kubernetes_deb_version"`
}

// ParseBuildConfig takes a build configuration file and converts it into a BuildConfig
func ParseBuildConfig(configFilePath string) *BuildConfig {
log.Println("parsing configuration file")
var buildconfig *BuildConfig

configFile, err := os.ReadFile(configFilePath)
if err != nil {
log.Fatalln(err)
}

err = json.Unmarshal(configFile, &buildconfig)
if err != nil {
log.Fatalln(err)
}
return buildconfig
}
31 changes: 30 additions & 1 deletion pkg/openstack/openstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/gophercloud/gophercloud/openstack"
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/floatingips"
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/keypairs"
"github.com/gophercloud/gophercloud/openstack/compute/v2/flavors"
"github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
"log"
"strings"
Expand Down Expand Up @@ -83,10 +84,12 @@ func (c *Client) CreateKeypair() *keypairs.KeyPair {
}

// CreateServer creates a compute instance in Openstack.
func (c *Client) CreateServer(keypair *keypairs.KeyPair, imageID, serverFlavorID, networkID string, enableConfigDrive bool) (*servers.Server, string) {
func (c *Client) CreateServer(keypair *keypairs.KeyPair, imageID, flavorName, networkID string, enableConfigDrive bool) (*servers.Server, string) {
trivyVersion := "0.31.3"
client := createComputeClient(c)

serverFlavorID := c.GetFlavorIDByName(flavorName)

serverOpts := servers.CreateOpts{
Name: "Scanner",
FlavorRef: serverFlavorID,
Expand Down Expand Up @@ -145,6 +148,32 @@ func (c *Client) RemoveKeypair() {
}
}

// GetFlavorIDByName will take a name of a flavor and attempt to find the ID from Openstack.
func (c *Client) GetFlavorIDByName(name string) string {
client := createComputeClient(c)

listOpts := flavors.ListOpts{
AccessType: flavors.PublicAccess,
}

allPages, err := flavors.ListDetail(client, listOpts).AllPages()
if err != nil {
panic(err)
}

allFlavors, err := flavors.ExtractFlavors(allPages)
if err != nil {
panic(err)
}

for _, flavor := range allFlavors {
if flavor.Name == name {
return flavor.ID
}
}
return ""
}

// attachFloatingIP will find the first free IP available and attach it to the instance.
// If it cannot find one, it will error.
func attachFloatingIP(client *gophercloud.ServiceClient, serverID string) string {
Expand Down

0 comments on commit cc595de

Please sign in to comment.