Skip to content

Commit

Permalink
fix: updated key and server name to be more dynamic allowing multiple…
Browse files Browse the repository at this point in the history
… runs in pipeline
  • Loading branch information
drew-viles committed Nov 2, 2022
1 parent ff9470d commit bb26868
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 53 deletions.
10 changes: 6 additions & 4 deletions cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ func fetchBuildRepo(imageRepo string) string {
// generateVariablesFile builds a variables file from the struct.
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)
outputFileName := strings.Join([]string{"tmp", ".json"}, "")
outputFile := filepath.Join(buildGitDir, "images/capi/", outputFileName)

configContent, err := json.Marshal(buildConfig)
if err != nil {
Expand All @@ -85,7 +85,7 @@ func fetchDependencies(repoPath string) {

wr := io.MultiWriter(w, os.Stdout)

err = systemUtils.RunMake("deps-openstack", repoPath, wr)
err = systemUtils.RunMake("deps-openstack", repoPath, nil, wr)
if err != nil {
log.Fatalln(err)
}
Expand Down Expand Up @@ -115,7 +115,9 @@ func buildImage(capiPath string, buildOS string) error {

args := strings.Join([]string{"build-openstack", buildOS}, "-")

err = systemUtils.RunMake(args, capiPath, wr)
env := []string{"PACKER_VAR_FILES=tmp.json"}
env = append(env, os.Environ()...)
err = systemUtils.RunMake(args, capiPath, env, wr)
if err != nil {
log.Fatalln(err)
}
Expand Down
76 changes: 37 additions & 39 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,56 +55,54 @@ func init() {
This tool has been designed to automatically build images for the Openstack potion of the Kubernetes Image Builder.
It could be extended out to provide images for a variety of other builders however for now it's main goal is to work with Openstack.`,
Run: func(cmd *cobra.Command, args []string) {
// Dump all the input vars into here.
envs := constants.Env{
AuthURL: osAuthURLFlag,
ProjectID: osProjectIDFlag,
ProjectName: osProjectNameFlag,
Username: osUsernameFlag,
Password: osPasswordFlag,
Region: osRegionNameFlag,
Interface: osInterfaceFlag,
UserDomainName: osUserDomainNameFlag,
ProjectDomainName: osProjectDomainNameFlag,
IdentityAPIVersion: osIdentityAPIVersionFlag,
AuthPlugin: osAuthPluginFlag,
NetworkID: networkIDFlag,
OpenstackBuildConfigPath: openstackBuildConfigPathFlag,
EnableConfigDrive: fmt.Sprintf("%t", enableConfigDriveFlag),
ImageRepo: imageRepoFlag,
BuildOS: buildOSFlag,
GhUser: ghUserFlag,
GhProject: ghProjectFlag,
GhToken: ghTokenFlag,
GhPagesBranch: ghPagesBranchFlag,
scanDate := fmt.Sprintf("%d-%d-%d--%d-%d-%d", time.Now().Year(), time.Now().Month(), time.Now().Day(), time.Now().Hour(), time.Now().Minute(), time.Now().Second())
imageUUID, err := uuid.NewUUID()
if err != nil {
log.Fatalln(err)
}

// Now we check to see if any env vars have been passed instead of flags. If so, set the flags to the env vars.
envs.CheckForEnvVars()

osClient := &ostack.Client{
Env: envs,
Env: constants.Env{
AuthURL: osAuthURLFlag,
ProjectID: osProjectIDFlag,
ProjectName: osProjectNameFlag,
Username: osUsernameFlag,
Password: osPasswordFlag,
Region: osRegionNameFlag,
Interface: osInterfaceFlag,
UserDomainName: osUserDomainNameFlag,
ProjectDomainName: osProjectDomainNameFlag,
IdentityAPIVersion: osIdentityAPIVersionFlag,
AuthPlugin: osAuthPluginFlag,
NetworkID: networkIDFlag,
OpenstackBuildConfigPath: openstackBuildConfigPathFlag,
EnableConfigDrive: fmt.Sprintf("%t", enableConfigDriveFlag),
ImageRepo: imageRepoFlag,
BuildOS: buildOSFlag,
GhUser: ghUserFlag,
GhProject: ghProjectFlag,
GhToken: ghTokenFlag,
GhPagesBranch: ghPagesBranchFlag,
},
}
// Now we check to see if any env vars have been passed instead of flags. If so, set the flags to the env vars.
osClient.Env.CheckForEnvVars()

buildConfig := ostack.ParseBuildConfig(envs.OpenstackBuildConfigPath)
buildConfig.Networks = envs.NetworkID

//Build image
buildGitDir := fetchBuildRepo(envs.ImageRepo)
buildConfig := ostack.ParseBuildConfig(osClient.Env.OpenstackBuildConfigPath)
buildConfig.Networks = osClient.Env.NetworkID

scanDate := fmt.Sprintf("%d-%d-%d--%d-%d-%d", time.Now().Year(), time.Now().Month(), time.Now().Day(), time.Now().Hour(), time.Now().Minute(), time.Now().Second())
imageUUID, err := uuid.NewUUID()
if err != nil {
log.Fatalln(err)
}
imageName := fmt.Sprintf("%s-kube-%s-%s-%s", buildConfig.BuildName, buildConfig.KubernetesSemver, scanDate, imageUUID.String())
imageName := fmt.Sprintf("%s-kube-%s-%s-%s", osClient.Env.BuildOS, buildConfig.KubernetesSemver, scanDate, imageUUID.String())
osClient.Env.ImageNameUUID = imageUUID.String()
buildConfig.ImageName = imageName

//Build image
buildGitDir := fetchBuildRepo(osClient.Env.ImageRepo)

generateVariablesFile(buildGitDir, buildConfig)

capiPath := filepath.Join(buildGitDir, "images/capi")
fetchDependencies(capiPath)
err = buildImage(capiPath, envs.BuildOS)
err = buildImage(capiPath, osClient.Env.BuildOS)
if err != nil {
log.Fatalln(err)
}
Expand All @@ -130,7 +128,7 @@ func init() {
removeScanningResources(server.ID, osClient)

//GitHub pages
pagesGitDir, pagesRepo, err := fetchPagesRepo(envs.GhUser, envs.GhToken, envs.GhProject, envs.GhPagesBranch)
pagesGitDir, pagesRepo, err := fetchPagesRepo(osClient.Env.GhUser, osClient.Env.GhToken, osClient.Env.GhProject, osClient.Env.GhPagesBranch)
if err != nil {
log.Fatalln(err)
}
Expand Down
1 change: 1 addition & 0 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ type Env struct {
OpenstackBuildConfigPath string
EnableConfigDrive string
ImageRepo string
ImageNameUUID string
BuildOS string
GhUser string
GhProject string
Expand Down
6 changes: 0 additions & 6 deletions pkg/openstack/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,7 @@ import (
)

type BuildConfig struct {
BuildName string `json:"build_name"`
ImageName string `json:"image_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"`
Expand Down
6 changes: 3 additions & 3 deletions pkg/openstack/openstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (c *Client) CreateKeypair() *keypairs.KeyPair {
client.Microversion = "2.2"

kp, err := keypairs.Create(client, keypairs.CreateOpts{
Name: "go-key",
Name: c.Env.ImageNameUUID + "-go-key",
Type: "ssh",
}).Extract()
if err != nil {
Expand All @@ -91,7 +91,7 @@ func (c *Client) CreateServer(keypair *keypairs.KeyPair, imageID, flavorName, ne
serverFlavorID := c.GetFlavorIDByName(flavorName)

serverOpts := servers.CreateOpts{
Name: "Scanner",
Name: c.Env.ImageNameUUID + "-scanner",
FlavorRef: serverFlavorID,
ImageRef: imageID,
SecurityGroups: []string{"default"},
Expand Down Expand Up @@ -142,7 +142,7 @@ func (c *Client) RemoveServer(serverID string) {
func (c *Client) RemoveKeypair() {
log.Println("removing keypair.")
client := createComputeClient(c)
res := keypairs.Delete(client, "go-key", keypairs.DeleteOpts{})
res := keypairs.Delete(client, c.Env.ImageNameUUID+"-go-key", keypairs.DeleteOpts{})
if res.Err != nil {
log.Println(res.Err)
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/system/make.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ import (
// RunMake simply runs the make command on the system with optional arguments and output locations.
// generally speaking the os.Stdout will be used but the option is there to write to a file
// in case parsing needs to happen after.
func RunMake(makeArgs, path string, output io.Writer) error {
func RunMake(makeArgs, path string, env []string, output io.Writer) error {
ctx, cancel := context.WithTimeout(context.Background(), 25*time.Minute)
defer cancel()

cmd := exec.CommandContext(ctx, "make", makeArgs)
cmd.Dir = path
cmd.Env = env
cmd.Stdout = output
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
Expand Down

0 comments on commit bb26868

Please sign in to comment.