Skip to content

Commit

Permalink
Merge "dev prepare - Ensure we get demo-tenant properly configured"
Browse files Browse the repository at this point in the history
  • Loading branch information
Microzuul CI authored and Gerrit Code Review committed Jan 16, 2024
2 parents 81453a0 + 4367fab commit 5eaf662
Show file tree
Hide file tree
Showing 11 changed files with 254 additions and 204 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,61 +15,24 @@ import (
var zuuldropindir = "zuul.d"
var zuulplaybooks = "playbooks"

var zuulrootdir = ""

func createDirectoryStructure(path string) error {

for _, dir := range []string{path, filepath.Join(path, zuulplaybooks), filepath.Join(path, zuuldropindir)} {
_, err := os.Stat(dir)
if err != nil && os.IsNotExist(err) {
if err := os.Mkdir(dir, 0755); err != nil {
return fmt.Errorf(err.Error())
}
if err := os.MkdirAll(dir, 0755); err != nil {
return fmt.Errorf(err.Error())
}
}

return nil
}

func writeOrAppendFile[F any](filestructure F, path string) error {
_, err := os.Stat(path)
if err != nil && os.IsNotExist(err) {
dataOutput, _ := yaml.Marshal(filestructure)
if err := os.WriteFile(path, dataOutput, 0666); err != nil {
return fmt.Errorf(err.Error())
}
func writeFile[F any](filestructure F, path string) error {
dataOutput, _ := yaml.Marshal(filestructure)
if err := os.WriteFile(path, dataOutput, 0666); err != nil {
return fmt.Errorf(err.Error())
}

if err == nil {
dataOutput, _ := yaml.Marshal(filestructure)
file, err := os.OpenFile(path, os.O_APPEND|os.O_WRONLY, os.ModeAppend)
if err != nil {
return fmt.Errorf(err.Error())
}
if _, err := file.WriteString(string(dataOutput)); err != nil {
return fmt.Errorf(err.Error())
}
}

return nil
}

func zuulWriteToFile[F utils.TenantConfig | utils.ProjectConfig | utils.PipelineConfig | utils.JobConfig](filestructure F, path string) error {
return writeOrAppendFile[F](filestructure, path)
}

func ansibleWriteToFile[F utils.AnsiblePlayBook](filestructure F, path string) error {
return writeOrAppendFile[F](filestructure, path)
}

func createZuulPipelineFile(pipelines utils.PipelineConfig, path string) error {
return zuulWriteToFile[utils.PipelineConfig](pipelines, path)
}

func createZuulJobFile(jobs utils.JobConfig, path string) error {
return zuulWriteToFile[utils.JobConfig](jobs, path)
}

func getAnsibleIncludeRole(rolename string) map[string]any {
return map[string]any{
"import_role": map[string]string{
Expand Down Expand Up @@ -105,160 +68,182 @@ Note: If the directories does not exit they will be created
driver, _ := cmd.Flags().GetString("driver")
outpath, _ := cmd.Flags().GetString("outpath")

zuulrootdir = outpath
InitConfigRepo(driver, connection, outpath)

if err := createDirectoryStructure(zuulrootdir); err != nil {
fmt.Println(err)
}
fmt.Println("Files generated at ", outpath)
},
}

// Check Pipeline
requireCheck, err := utils.GetRequireCheckByDriver(driver, connection)
if err != nil {
fmt.Println(err)
}
triggerCheck, err := utils.GetTriggerCheckByDriver(driver, connection)
if err != nil {
fmt.Println(err)
}
reportersCheck, err := utils.GetReportersCheckByDriver(driver, connection)
if err != nil {
fmt.Println(err)
}
// Gate Pipeline
requireGate, err := utils.GetRequireGateByDriver(driver, connection)
if err != nil {
fmt.Println(err)
}
triggerGate, err := utils.GetTriggerGateByDriver(driver, connection)
if err != nil {
fmt.Println(err)
}
reportersGate, err := utils.GetReportersGateByDriver(driver, connection)
if err != nil {
fmt.Println(err)
}
// Post Pipeline
triggerPost, err := utils.GetTriggerPostByDriver(driver, connection)
if err != nil {
fmt.Println(err)
}
func InitConfigRepo(driver string, connection string, zuulrootdir string) {

if err := createDirectoryStructure(zuulrootdir); err != nil {
fmt.Println(err)
}

// Check Pipeline
requireCheck, err := utils.GetRequireCheckByDriver(driver, connection)
if err != nil {
fmt.Println(err)
}
triggerCheck, err := utils.GetTriggerCheckByDriver(driver, connection)
if err != nil {
fmt.Println(err)
}
reportersCheck, err := utils.GetReportersCheckByDriver(driver, connection)
if err != nil {
fmt.Println(err)
}
// Gate Pipeline
requireGate, err := utils.GetRequireGateByDriver(driver, connection)
if err != nil {
fmt.Println(err)
}
triggerGate, err := utils.GetTriggerGateByDriver(driver, connection)
if err != nil {
fmt.Println(err)
}
reportersGate, err := utils.GetReportersGateByDriver(driver, connection)
if err != nil {
fmt.Println(err)
}
// Post Pipeline
triggerPost, err := utils.GetTriggerPostByDriver(driver, connection)
if err != nil {
fmt.Println(err)
}

zuulpipelinefilepath := filepath.Join(zuulrootdir, zuuldropindir, connection+"-pipeline.yaml")
if err := createZuulPipelineFile(utils.PipelineConfig{
{
Pipeline: utils.PipelineBody{
Name: "check",
Description: `Newly uploaded patchsets enter this
zuulpipelinefilepath := filepath.Join(zuulrootdir, zuuldropindir, connection+"-pipeline.yaml")
if err := writeFile(utils.PipelineConfig{
{
Pipeline: utils.PipelineBody{
Name: "check",
Description: `Newly uploaded patchsets enter this
pipeline to receive an initial +/-1 Verified vote.`,
Manager: utils.Independent,
Require: requireCheck,
Trigger: triggerCheck,
Start: reportersCheck[0],
Success: reportersCheck[1],
Failure: reportersCheck[2],
},
Manager: utils.Independent,
Require: requireCheck,
Trigger: triggerCheck,
Start: reportersCheck[0],
Success: reportersCheck[1],
Failure: reportersCheck[2],
},
{
Pipeline: utils.PipelineBody{
Name: "gate",
Description: `Changes that have been approved by core developers are enqueued in order in this pipeline, and if they pass tests, will be merged.`,
SuccessMessage: "Build succeeded (gate pipeline).",
FailureMessage: "Build failed (gate pipeline).",
Precedence: utils.GetZuulPipelinePrecedence("high"),
Supercedes: []string{"check"},
PostReview: true,
Manager: utils.Dependent,
Require: requireGate,
Trigger: triggerGate,
Start: reportersGate[0],
Success: reportersGate[1],
Failure: reportersGate[2],
},
},
{
Pipeline: utils.PipelineBody{
Name: "gate",
Description: `Changes that have been approved by core developers are enqueued in order in this pipeline, and if they pass tests, will be merged.`,
SuccessMessage: "Build succeeded (gate pipeline).",
FailureMessage: "Build failed (gate pipeline).",
Precedence: utils.GetZuulPipelinePrecedence("high"),
Supercedes: []string{"check"},
PostReview: true,
Manager: utils.Dependent,
Require: requireGate,
Trigger: triggerGate,
Start: reportersGate[0],
Success: reportersGate[1],
Failure: reportersGate[2],
},
{
Pipeline: utils.PipelineBody{
Name: "post",
PostReview: true,
Description: `This pipeline runs jobs that operate after each change is merged.`,
Manager: utils.Supercedent,
Precedence: utils.GetZuulPipelinePrecedence("low"),
Trigger: triggerPost,
},
},
{
Pipeline: utils.PipelineBody{
Name: "post",
PostReview: true,
Description: `This pipeline runs jobs that operate after each change is merged.`,
Manager: utils.Supercedent,
Precedence: utils.GetZuulPipelinePrecedence("low"),
Trigger: triggerPost,
},
}, zuulpipelinefilepath); err != nil {
fmt.Println(err)
}
},
}, zuulpipelinefilepath); err != nil {
fmt.Println(err)
}

zuuljobfilepath := filepath.Join(zuulrootdir, zuuldropindir, connection+"-base-jobs.yaml")
if err := createZuulJobFile(utils.JobConfig{
{
Job: utils.JobBody{
Name: "base",
Description: "The base job.",
Parent: nil,
PreRun: []string{
"playbooks/" + connection + "-pre.yaml",
},
Roles: []utils.JobRoles{
map[string]string{
"zuul": "zuul/zuul-jobs",
},
zuuljobfilepath := filepath.Join(zuulrootdir, zuuldropindir, connection+"-base-jobs.yaml")
if err := writeFile(utils.JobConfig{
{
Job: utils.JobBody{
Name: "base",
Description: "The base job.",
Parent: nil,
PreRun: []string{
"playbooks/" + connection + "-pre.yaml",
},
Roles: []utils.JobRoles{
map[string]string{
"zuul": "zuul/zuul-jobs",
},
Timeout: 1800,
Attempts: 3,
},
Timeout: 1800,
Attempts: 3,
},
}, zuuljobfilepath); err != nil {
fmt.Println(err)
}
},
}, zuuljobfilepath); err != nil {
fmt.Println(err)
}

zuuljobplaybookfilepath := filepath.Join(zuulrootdir, zuulplaybooks, connection+"-pre.yaml")
if err := ansibleWriteToFile(utils.AnsiblePlayBook{
{
Hosts: "localhost",
Tasks: []map[string]any{
{
"block": []map[string]any{
getAnsibleIncludeRole("emit-job-header"),
getAnsibleIncludeRole("log-inventory"),
},
zuuljobplaybookfilepath := filepath.Join(zuulrootdir, zuulplaybooks, connection+"-pre.yaml")
if err := writeFile(utils.AnsiblePlayBook{
{
Hosts: "localhost",
Tasks: []map[string]any{
{
"block": []map[string]any{
getAnsibleIncludeRole("emit-job-header"),
getAnsibleIncludeRole("log-inventory"),
},
},
},
{
Hosts: "all",
Tasks: []map[string]any{
getAnsibleIncludeRole("start-zuul-console"),
{
"block": []map[string]any{
getAnsibleIncludeRole("validate-host"),
getAnsibleIncludeRole("prepare-workspace"),
getAnsibleIncludeRole("add-build-sshkey"),
},
"when": `'ansible_connection != "kubectl"'`,
},
{
Hosts: "all",
Tasks: []map[string]any{
getAnsibleIncludeRole("start-zuul-console"),
{
"block": []map[string]any{
getAnsibleIncludeRole("validate-host"),
getAnsibleIncludeRole("prepare-workspace"),
getAnsibleIncludeRole("add-build-sshkey"),
},
{
"block": []map[string]any{
getAnsibleIncludeRole("prepare-workspace-openshift"),
getAnsibleIncludeRole("remove-zuul-sshkey"),
},
"run_once": true,
"when": "ansible_connection != 'kubectl'",
"when": `'ansible_connection != "kubectl"'`,
},
{
"block": []map[string]any{
getAnsibleIncludeRole("prepare-workspace-openshift"),
getAnsibleIncludeRole("remove-zuul-sshkey"),
},
{
"import_role": map[string]string{
"name": "ensure-output-dirs",
},
"when": "ansible_user_dir is defined",
"run_once": true,
"when": "ansible_connection != 'kubectl'",
},
{
"import_role": map[string]string{
"name": "ensure-output-dirs",
},
"when": "ansible_user_dir is defined",
},
},
}, zuuljobplaybookfilepath); err != nil {
fmt.Println(err)
}
},
}, zuuljobplaybookfilepath); err != nil {
fmt.Println(err)
}

fmt.Println("Files generated at ", outpath)
},
zuulppfilepath := filepath.Join(zuulrootdir, zuuldropindir, connection+"-project-pipeline.yaml")
if err := writeFile(utils.ProjectConfig{{
Project: utils.ZuulProjectBody{
Pipeline: utils.ZuulProjectPipelineMap{
"check": utils.ZuulProjectPipeline{
Jobs: []string{
"noop",
},
},
"gate": utils.ZuulProjectPipeline{
Jobs: []string{
"noop",
},
},
},
}}}, zuulppfilepath); err != nil {
fmt.Println(err)
}
}

func init() {
Expand Down
Loading

0 comments on commit 5eaf662

Please sign in to comment.