Skip to content

Feature: Ansible directory path inside repo #2766

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 16 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion api-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ swagger: '2.0'
info:
title: SEMAPHORE
description: Semaphore API
version: "2.2.0"
version: "2.2.1"

host: localhost:3000

Expand Down Expand Up @@ -231,6 +231,8 @@ definitions:
type: string
ssh_key:
type: string
path:
type: string
keys:
type: array
items:
Expand Down Expand Up @@ -672,6 +674,9 @@ definitions:
example: master
ssh_key_id:
type: integer
path:
type: string
example: deployment/ansible
Repository:
type: object
properties:
Expand All @@ -690,6 +695,9 @@ definitions:
example: master
ssh_key_id:
type: integer
path:
type: string
example: deployment/ansible

Task:
type: object
Expand Down
10 changes: 8 additions & 2 deletions db/Inventory.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package db

import "path"

type InventoryType string

const (
Expand Down Expand Up @@ -42,11 +44,15 @@ type Inventory struct {
}

func (e Inventory) GetFilename() string {
pathPrefix := ""
if e.Repository != nil {
pathPrefix = e.Repository.Path
}
if e.Type != InventoryFile {
return ""
return pathPrefix
}

return e.Inventory
return path.Join(pathPrefix, e.Inventory)

//return strings.TrimPrefix(e.Inventory, "/")
}
Expand Down
1 change: 1 addition & 0 deletions db/Migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func GetMigrations() []Migration {
{Version: "2.12.3"},
{Version: "2.12.4"},
{Version: "2.12.5"},
{Version: "2.12.6"},
}
}

Expand Down
5 changes: 5 additions & 0 deletions db/Repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Repository struct {
SSHKeyID int `db:"ssh_key_id" json:"ssh_key_id" binding:"required" backup:"-"`

SSHKey AccessKey `db:"-" json:"-" backup:"-"`
Path string `db:"path" json:"path" backup:"-"`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

backup:"-" - must be backup:"path".

}

func (r Repository) ClearCache() error {
Expand Down Expand Up @@ -68,6 +69,10 @@ func (r Repository) GetDirName(templateID int) string {
return r.getDirNamePrefix() + strconv.Itoa(templateID)
}

func (r Repository) GetAnsiblePath(templateID int) string {
return path.Join(r.GetFullPath(templateID), r.Path)
}

func (r Repository) GetFullPath(templateID int) string {
if r.GetType() == RepositoryLocal {
return r.GetGitURL()
Expand Down
1 change: 1 addition & 0 deletions db/sql/migrations/v2.12.6.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alter table `project__repository` add column path text not null;
8 changes: 5 additions & 3 deletions db/sql/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,12 @@ func (d *SqlDb) UpdateRepository(repository db.Repository) error {
}

_, err = d.exec(
"update project__repository set name=?, git_url=?, git_branch=?, ssh_key_id=? where id=?",
"update project__repository set name=?, git_url=?, git_branch=?, ssh_key_id=?, path=? where id=?",
repository.Name,
repository.GitURL,
repository.GitBranch,
repository.SSHKeyID,
repository.Path,
repository.ID)

return err
Expand All @@ -82,12 +83,13 @@ func (d *SqlDb) CreateRepository(repository db.Repository) (newRepo db.Repositor

insertID, err := d.insert(
"id",
"insert into project__repository(project_id, git_url, git_branch, ssh_key_id, name) values (?, ?, ?, ?, ?)",
"insert into project__repository(project_id, git_url, git_branch, ssh_key_id, name, path) values (?, ?, ?, ?, ?, ?)",
repository.ProjectID,
repository.GitURL,
repository.GitBranch,
repository.SSHKeyID,
repository.Name)
repository.Name,
repository.Path)

if err != nil {
return
Expand Down
2 changes: 1 addition & 1 deletion db_lib/AnsibleApp.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (t *AnsibleApp) getRepoPath() string {
Client: CreateDefaultGitClient(),
}

return repo.GetFullPath()
return repo.GetAnsiblePath()
}

func (t *AnsibleApp) installGalaxyRequirementsFile(requirementsType GalaxyRequirementsType, requirementsFilePath string) error {
Expand Down
6 changes: 3 additions & 3 deletions db_lib/AnsiblePlaybook.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type AnsiblePlaybook struct {

func (p AnsiblePlaybook) makeCmd(command string, args []string, environmentVars []string) *exec.Cmd {
cmd := exec.Command(command, args...) //nolint: gas
cmd.Dir = p.GetFullPath()
cmd.Dir = p.getAnsiblePath()

cmd.Env = append(cmd.Env, "PYTHONUNBUFFERED=1")
cmd.Env = append(cmd.Env, "ANSIBLE_FORCE_COLOR=True")
Expand Down Expand Up @@ -82,7 +82,7 @@ func (p AnsiblePlaybook) RunGalaxy(args []string) error {
return p.runCmd("ansible-galaxy", args)
}

func (p AnsiblePlaybook) GetFullPath() (path string) {
path = p.Repository.GetFullPath(p.TemplateID)
func (p AnsiblePlaybook) getAnsiblePath() (path string) {
path = p.Repository.GetAnsiblePath(p.TemplateID)
return
}
4 changes: 4 additions & 0 deletions db_lib/GitRepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ type GitRepository struct {
Client GitClient
}

func (r GitRepository) GetAnsiblePath() string {
return path.Join(r.GetFullPath(), r.Repository.Path)
}

func (r GitRepository) GetFullPath() string {
if r.TmpDirName != "" {
return path.Join(util.Config.TmpPath, r.TmpDirName)
Expand Down
2 changes: 1 addition & 1 deletion db_lib/TerraformApp.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (t *TerraformApp) runCmd(command string, args []string) error {
}

func (t *TerraformApp) GetFullPath() string {
return path.Join(t.Repository.GetFullPath(t.Template.ID), strings.TrimPrefix(t.Template.Playbook, "/"))
return path.Join(t.Repository.GetAnsiblePath(t.Template.ID), strings.TrimPrefix(t.Template.Playbook, "/"))
}

func (t *TerraformApp) SetLogger(logger task_logger.Logger) task_logger.Logger {
Expand Down
1 change: 1 addition & 0 deletions services/project/backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func TestBackupProject(t *testing.T) {
Name: "Test",
GitURL: "[email protected]:test/test",
GitBranch: "master",
Path: "deployment/ansible",
})
assert.NoError(t, err)

Expand Down
19 changes: 19 additions & 0 deletions web/src/components/RepositoryForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@
</v-tooltip>
</template>
</v-select>

<v-text-field
v-model.trim="item.path"
:label="$t('path')"
:rules="[v => isValidPath(v) != false || $t('invalidPath')]"
required
:disabled="formSaving || type === 'local'"
></v-text-field>

</v-form>
</template>
<script>
Expand Down Expand Up @@ -186,6 +195,16 @@ export default {
getSingleItemUrl() {
return `/api/project/${this.projectId}/repositories/${this.itemId}`;
},

isValidPath(path) {
// TODO Write this.
// Always accept an empty path
if (path == null || path === '') {
return true;
}
// For now, always accept any path
return true;
},
},
};
</script>
1 change: 1 addition & 0 deletions web/src/lang/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,4 +306,5 @@ export default {
status_success: 'Success',
status_failed: 'Failed',
status_stopped: 'Stopped',
invalidPath: 'Invalid path',
};
13 changes: 11 additions & 2 deletions web/src/views/project/Repositories.vue
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@
{{ keys.find((k) => k.id === item.ssh_key_id).name }}
</template>

<template v-slot:item.path="{ item }">
{{ item.path }}
</template>

<template v-slot:item.actions="{ item }">
<v-btn-toggle dense :value-comparator="() => false">
<v-btn @click="askDeleteItem(item.id)">
Expand Down Expand Up @@ -104,7 +108,7 @@ export default {
return [{
text: this.$i18n.t('name'),
value: 'name',
width: '25%',
width: '20%',
},
{
text: this.$i18n.t('gitUrl'),
Expand All @@ -114,7 +118,12 @@ export default {
{
text: this.$i18n.t('sshKey'),
value: 'ssh_key_id',
width: '25%',
width: '15%',
},
{
text: this.$i18n.t('path'),
value: 'path',
width: '15%',
},
{
text: this.$i18n.t('actions'),
Expand Down
Loading