Skip to content

Commit

Permalink
feat(be): add ability to override template extra args in task #832
Browse files Browse the repository at this point in the history
  • Loading branch information
fiftin committed Jan 26, 2022
1 parent a68c64c commit 6799208
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .dredd/hooks/capabilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func resolveCapability(caps []string, resolved []string, uid string) {
case "template":
res, err := store.Sql().Exec(
"insert into project__template "+
"(project_id, inventory_id, repository_id, environment_id, alias, playbook, arguments, override_args, description, view_id) "+
"(project_id, inventory_id, repository_id, environment_id, alias, playbook, arguments, allow_override_args_in_task, description, view_id) "+
"values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
userProject.ID, inventoryID, repoID, environmentID, "Test-"+uid, "test-playbook.yml", "", false, "Hello, World!", view.ID)
printError(err)
Expand Down
6 changes: 4 additions & 2 deletions api-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,9 @@ definitions:
description:
type: string
example: Hello, World!
override_args:
allow_override_args_in_task:
type: boolean
example: false
Template:
type: object
properties:
Expand Down Expand Up @@ -372,8 +373,9 @@ definitions:
description:
type: string
example: Hello, World!
override_args:
allow_override_args_in_task:
type: boolean
example: false

ScheduleRequest:
type: object
Expand Down
19 changes: 13 additions & 6 deletions api/tasks/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -782,18 +782,25 @@ func (t *task) getPlaybookArgs() (args []string, err error) {
if t.template.Arguments != nil {
err = json.Unmarshal([]byte(*t.template.Arguments), &templateExtraArgs)
if err != nil {
t.log("Could not unmarshal arguments to []string")
t.log("Invalid format of the template extra arguments, must be valid JSON")
return
}
}

if t.template.OverrideArguments {
args = templateExtraArgs
} else {
args = append(args, templateExtraArgs...)
args = append(args, playbookName)
var taskExtraArgs []string

if t.template.AllowOverrideArgsInTask && t.task.Arguments != nil {
err = json.Unmarshal([]byte(*t.task.Arguments), &taskExtraArgs)
if err != nil {
t.log("Invalid format of the task extra arguments, must be valid JSON")
return
}
}

args = append(args, templateExtraArgs...)
args = append(args, taskExtraArgs...)
args = append(args, playbookName)

return
}

Expand Down
1 change: 1 addition & 0 deletions db/Migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func GetMigrations() []Migration {
{Version: "2.8.20"},
{Version: "2.8.25"},
{Version: "2.8.26"},
{Version: "2.8.36"},
}
}

Expand Down
4 changes: 4 additions & 0 deletions db/Task.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ type Task struct {

Message string `db:"message" json:"message"`

// CommitMessage is a git commit hash of playbook repository which
// was active when task was created.
CommitHash *string `db:"commit_hash" json:"commit_hash"`
// CommitMessage contains message retrieved from git repository after checkout to CommitHash.
// It is readonly by API.
Expand All @@ -37,6 +39,8 @@ type Task struct {
// Version is a build version.
// This field available only for Build tasks.
Version *string `db:"version" json:"version"`

Arguments *string `db:"arguments" json:"arguments"`
}

func (task *Task) GetIncomingVersion(d Store) *string {
Expand Down
2 changes: 1 addition & 1 deletion db/Template.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type Template struct {
// to fit into []string
Arguments *string `db:"arguments" json:"arguments"`
// if true, semaphore will not prepend any arguments to `arguments` like inventory, etc
OverrideArguments bool `db:"override_args" json:"override_args"`
AllowOverrideArgsInTask bool `db:"allow_override_args_in_task" json:"allow_override_args_in_task"`

Removed bool `db:"removed" json:"-"`

Expand Down
3 changes: 3 additions & 0 deletions db/sql/migrations/v2.8.36.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
alter table `project__template` add allow_override_args_in_task bool not null default false;
alter table `task` add arguments text;
alter table `project__template` drop column `override_args`;
10 changes: 5 additions & 5 deletions db/sql/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func (d *SqlDb) CreateTemplate(template db.Template) (newTemplate db.Template, e
insertID, err := d.insert(
"id",
"insert into project__template (project_id, inventory_id, repository_id, environment_id, "+
"alias, playbook, arguments, override_args, description, vault_key_id, `type`, start_version,"+
"alias, playbook, arguments, allow_override_args_in_task, description, vault_key_id, `type`, start_version,"+
"build_template_id, view_id, autorun, survey_vars)"+
"values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
template.ProjectID,
Expand All @@ -26,7 +26,7 @@ func (d *SqlDb) CreateTemplate(template db.Template) (newTemplate db.Template, e
template.Alias,
template.Playbook,
template.Arguments,
template.OverrideArguments,
template.AllowOverrideArgsInTask,
template.Description,
template.VaultKeyID,
template.Type,
Expand Down Expand Up @@ -66,7 +66,7 @@ func (d *SqlDb) UpdateTemplate(template db.Template) error {
"alias=?, "+
"playbook=?, "+
"arguments=?, "+
"override_args=?, "+
"allow_override_args_in_task=?, "+
"description=?, "+
"vault_key_id=?, "+
"`type`=?, "+
Expand All @@ -82,7 +82,7 @@ func (d *SqlDb) UpdateTemplate(template db.Template) error {
template.Alias,
template.Playbook,
template.Arguments,
template.OverrideArguments,
template.AllowOverrideArgsInTask,
template.Description,
template.VaultKeyID,
template.Type,
Expand All @@ -106,7 +106,7 @@ func (d *SqlDb) GetTemplates(projectID int, filter db.TemplateFilter, params db.
"pt.alias",
"pt.playbook",
"pt.arguments",
"pt.override_args",
"pt.allow_override_args_in_task",
"pt.vault_key_id",
"pt.view_id",
"pt.`type`").
Expand Down

0 comments on commit 6799208

Please sign in to comment.