refactor(be): remove unused columns

This commit is contained in:
Denis Gukov 2021-08-30 19:24:20 +05:00
parent 43654dcaff
commit 92895f9f3c
19 changed files with 57 additions and 121 deletions

View File

@ -73,19 +73,29 @@ func resolveCapability(caps []string, resolved []string, uid string) {
case "access_key":
userKey = addAccessKey(&userProject.ID)
case "repository":
pRepo, err := store.Sql().Exec("insert into project__repository set project_id=?, git_url=?, ssh_key_id=?, name=?", userProject.ID, "git@github.com/ansible,semaphore/semaphore", userKey.ID, "ITR-"+uid)
pRepo, err := store.Sql().Exec(
"insert into project__repository (project_id, git_url, ssh_key_id, name) values (?, ?, ?, ?)",
userProject.ID, "git@github.com/ansible,semaphore/semaphore", userKey.ID, "ITR-"+uid)
printError(err)
repoID, _ = pRepo.LastInsertId()
case "inventory":
res, err := store.Sql().Exec("insert into project__inventory set project_id=?, name=?, type=?, key_id=?, ssh_key_id=?, inventory=?", userProject.ID, "ITI-"+uid, "static", userKey.ID, userKey.ID, "Test Inventory")
res, err := store.Sql().Exec(
"insert into project__inventory (project_id, name, type, key_id, ssh_key_id, inventory) values (?, ?, ?, ?, ?, ?)",
userProject.ID, "ITI-"+uid, "static", userKey.ID, userKey.ID, "Test Inventory")
printError(err)
inventoryID, _ = res.LastInsertId()
case "environment":
res, err := store.Sql().Exec("insert into project__environment set project_id=?, name=?, json=?, password=?", userProject.ID, "ITI-"+uid, "{}", "test-pass")
res, err := store.Sql().Exec(
"insert into project__environment (project_id, name, json, password) values (?, ?, ?, ?)",
userProject.ID, "ITI-"+uid, "{}", "test-pass")
printError(err)
environmentID, _ = res.LastInsertId()
case "template":
res, err := store.Sql().Exec("insert into project__template set ssh_key_id=?, project_id=?, inventory_id=?, repository_id=?, environment_id=?, alias=?, playbook=?, arguments=?, override_args=?", userKey.ID, userProject.ID, inventoryID, repoID, environmentID, "Test-"+uid, "test-playbook.yml", "", false)
res, err := store.Sql().Exec(
"insert into project__template " +
"(ssh_key_id, project_id, inventory_id, repository_id, environment_id, alias, playbook, arguments, override_args) " +
"value (?, ?, ?, ?, ?, ?, ?, ?, ?)",
userKey.ID, userProject.ID, inventoryID, repoID, environmentID, "Test-"+uid, "test-playbook.yml", "", false)
printError(err)
templateID, _ = res.LastInsertId()
case "task":

View File

@ -85,7 +85,7 @@ func setupObjectsAndPaths(t *transaction.Transaction) {
// Object Lifecycle
func addUserProjectRelation(pid int, user int) {
_, err := store.Sql().Exec("insert into project__user set project_id=?, user_id=?, `admin`=1", pid, user)
_, err := store.Sql().Exec("insert into project__user (project_id, user_id, `admin`) values (?, ?, 1)", pid, user)
if err != nil {
fmt.Println(err)
}

View File

@ -145,7 +145,7 @@ definitions:
type: string
type:
type: string
enum: [ssh, aws, gcloud, do]
enum: [ssh,none]
project_id:
type: integer
minimum: 1
@ -164,7 +164,7 @@ definitions:
type: string
type:
type: string
enum: [ssh, aws, gcloud, do]
enum: [ssh]
project_id:
type: integer
key:
@ -872,7 +872,7 @@ paths:
in: query
required: false
type: string
enum: [ssh, aws, gcloud, do]
enum: [ssh]
description: Filter by key type
x-example: ssh
- name: sort

View File

@ -72,9 +72,9 @@ func AddKey(w http.ResponseWriter, r *http.Request) {
}
switch key.Type {
case "aws", "gcloud", "do":
case db.AccessKeyNone:
break
case "ssh":
case db.AccessKeySSH:
if key.Secret == nil || len(*key.Secret) == 0 {
helpers.WriteJSON(w, http.StatusBadRequest, map[string]string{
"error": "SSH Secret empty",
@ -88,7 +88,9 @@ func AddKey(w http.ResponseWriter, r *http.Request) {
return
}
*key.Secret += "\n"
if key.Secret != nil {
*key.Secret += "\n"
}
newKey, err := helpers.Store(r).CreateAccessKey(key)
@ -128,9 +130,9 @@ func UpdateKey(w http.ResponseWriter, r *http.Request) {
}
switch key.Type {
case "aws", "gcloud", "do":
case db.AccessKeyNone:
break
case "ssh":
case db.AccessKeySSH:
if key.Secret == nil || len(*key.Secret) == 0 {
helpers.WriteJSON(w, http.StatusBadRequest, map[string]string{
"error": "SSH Secret empty",

View File

@ -37,7 +37,7 @@ type task struct {
store db.Store
task db.Task
template db.Template
sshKey db.AccessKey
//sshKey db.AccessKey
inventory db.Inventory
repository db.Repository
environment db.Environment
@ -298,15 +298,15 @@ func (t *task) populateDetails() error {
}
// get access key
t.sshKey, err = t.store.GetAccessKey(t.template.ProjectID, t.template.SSHKeyID)
if err != nil {
return t.prepareError(err, "Template AccessKey not found!")
}
if t.sshKey.Type != "ssh" {
t.log("Non ssh-type keys are currently not supported: " + t.sshKey.Type)
return errors.New("unsupported SSH Key")
}
//t.sshKey, err = t.store.GetAccessKey(t.template.ProjectID, t.template.SSHKeyID)
//if err != nil {
// return t.prepareError(err, "Template AccessKey not found!")
//}
//
//if t.sshKey.Type != "ssh" {
// t.log("Non ssh-type keys are currently not supported: " + t.sshKey.Type)
// return errors.New("unsupported SSH Key")
//}
// get inventory
t.inventory, err = t.store.GetInventory(t.template.ProjectID, t.template.InventoryID)
@ -321,7 +321,7 @@ func (t *task) populateDetails() error {
return err
}
if t.repository.SSHKey.Type != "ssh" {
if t.repository.SSHKey.Type != db.AccessKeySSH {
t.log("Repository Access Key is not 'SSH': " + t.repository.SSHKey.Type)
return errors.New("unsupported SSH Key")
}
@ -491,7 +491,7 @@ func (t *task) getPlaybookArgs() ([]string, error) {
"-i", inventory,
}
if t.inventory.SSHKeyID != nil {
if t.inventory.SSHKeyID != nil && t.inventory.SSHKey.Type == db.AccessKeySSH {
args = append(args, "--private-key="+t.inventory.SSHKey.GetPath())
}

View File

@ -6,11 +6,17 @@ import (
"github.com/ansible-semaphore/semaphore/util"
)
const (
AccessKeySSH = "ssh"
AccessKeyNone = "none"
)
// AccessKey represents a key used to access a machine with ansible from semaphore
type AccessKey struct {
ID int `db:"id" json:"id"`
Name string `db:"name" json:"name" binding:"required"`
// 'aws/do/gcloud/ssh'
// 'ssh/none'
Type string `db:"type" json:"type" binding:"required"`
ProjectID *int `db:"project_id" json:"project_id"`

View File

@ -7,14 +7,11 @@ type Inventory struct {
ProjectID int `db:"project_id" json:"project_id"`
Inventory string `db:"inventory" json:"inventory"`
// accesses dynamic inventory
KeyID *int `db:"key_id" json:"key_id"`
Key AccessKey `db:"-" json:"-"`
// accesses hosts in inventory
SSHKeyID *int `db:"ssh_key_id" json:"ssh_key_id"`
SSHKey AccessKey `db:"-" json:"-"`
// static/aws/do/gcloud
// static/file
Type string `db:"type" json:"type"`
Removed bool `db:"removed" json:"removed"`

View File

@ -4,7 +4,6 @@ package db
type Template struct {
ID int `db:"id" json:"id"`
SSHKeyID int `db:"ssh_key_id" json:"ssh_key_id"`
ProjectID int `db:"project_id" json:"project_id"`
InventoryID int `db:"inventory_id" json:"inventory_id"`
RepositoryID int `db:"repository_id" json:"repository_id"`

View File

@ -12,13 +12,6 @@ func (d *BoltDb) GetInventory(projectID int, inventoryID int) (inventory db.Inve
return
}
if inventory.KeyID != nil {
inventory.Key, err = d.GetAccessKey(projectID, *inventory.KeyID)
if err != nil {
return
}
}
if inventory.SSHKeyID != nil {
inventory.SSHKey, err = d.GetAccessKey(projectID, *inventory.SSHKeyID)
}

View File

@ -77,5 +77,6 @@ func init() {
{Major: 2, Minor: 7, Patch: 1},
{Major: 2, Minor: 7, Patch: 4},
{Major: 2, Minor: 7, Patch: 6},
{Major: 2, Minor: 7, Patch: 8},
}
}

View File

@ -8,13 +8,6 @@ func (d *SqlDb) GetInventory(projectID int, inventoryID int) (inventory db.Inven
return
}
if inventory.KeyID != nil {
inventory.Key, err = d.GetAccessKey(projectID, *inventory.KeyID)
if err != nil {
return
}
}
if inventory.SSHKeyID != nil {
inventory.SSHKey, err = d.GetAccessKey(projectID, *inventory.SSHKeyID)
}
@ -38,10 +31,9 @@ func (d *SqlDb) DeleteInventorySoft(projectID int, inventoryID int) error {
func (d *SqlDb) UpdateInventory(inventory db.Inventory) error {
_, err := d.exec(
"update project__inventory set name=?, type=?, key_id=?, ssh_key_id=?, inventory=? where id=?",
"update project__inventory set name=?, type=?, ssh_key_id=?, inventory=? where id=?",
inventory.Name,
inventory.Type,
inventory.KeyID,
inventory.SSHKeyID,
inventory.Inventory,
inventory.ID)
@ -52,11 +44,10 @@ func (d *SqlDb) UpdateInventory(inventory db.Inventory) error {
func (d *SqlDb) CreateInventory(inventory db.Inventory) (newInventory db.Inventory, err error) {
insertID, err := d.insert(
"id",
"insert into project__inventory set project_id=?, name=?, type=?, key_id=?, ssh_key_id=?, inventory=?",
"insert into project__inventory (project_id, name, type, ssh_key_id, inventory) values (?, ?, ?, ?, ?)",
inventory.ProjectID,
inventory.Name,
inventory.Type,
inventory.KeyID,
inventory.SSHKeyID,
inventory.Inventory)

View File

@ -14,7 +14,6 @@ var (
dateTimeTypeRE = regexp.MustCompile(`(?i)\bdatetime\b`)
tinyintRE = regexp.MustCompile(`(?i)\btinyint\b`)
longtextRE = regexp.MustCompile(`(?i)\blongtext\b`)
columnModifyRE = regexp.MustCompile(`(?i)\bmodify\b`)
)
// prepareMigration converts migration SQLite-query to current dialect.
@ -29,7 +28,6 @@ func (d *SqlDb) prepareMigration(query string) string {
query = dateTimeTypeRE.ReplaceAllString(query, "timestamp")
query = tinyintRE.ReplaceAllString(query, "smallint")
query = longtextRE.ReplaceAllString(query, "text")
query = columnModifyRE.ReplaceAllString(query, "alter column")
}
return query
}

View File

@ -0,0 +1,3 @@
alter table `project__inventory` drop column `key_id`;
alter table `project__template` drop column `ssh_key_id`;

View File

@ -9,8 +9,8 @@ import (
func (d *SqlDb) CreateTemplate(template db.Template) (newTemplate db.Template, err error) {
insertID, err := d.insert(
"id",
"insert into project__template set ssh_key_id=?, project_id=?, inventory_id=?, repository_id=?, environment_id=?, alias=?, playbook=?, arguments=?, override_args=?",
template.SSHKeyID,
"insert into project__template (project_id, inventory_id, repository_id, environment_id, alias, playbook, arguments, override_args)" +
"value (?, ?, ?, ?, ?, ?, ?, ?)",
template.ProjectID,
template.InventoryID,
template.RepositoryID,
@ -30,8 +30,7 @@ func (d *SqlDb) CreateTemplate(template db.Template) (newTemplate db.Template, e
}
func (d *SqlDb) UpdateTemplate(template db.Template) error {
_, err := d.exec("update project__template set ssh_key_id=?, inventory_id=?, repository_id=?, environment_id=?, alias=?, playbook=?, arguments=?, override_args=? where id=?",
template.SSHKeyID,
_, err := d.exec("update project__template set inventory_id=?, repository_id=?, environment_id=?, alias=?, playbook=?, arguments=?, override_args=? where id=?",
template.InventoryID,
template.RepositoryID,
template.EnvironmentID,
@ -46,7 +45,6 @@ func (d *SqlDb) UpdateTemplate(template db.Template) error {
func (d *SqlDb) GetTemplates(projectID int, params db.RetrieveQueryParams) (templates []db.Template, err error) {
q := squirrel.Select("pt.id",
"pt.ssh_key_id",
"pt.project_id",
"pt.inventory_id",
"pt.repository_id",
@ -66,10 +64,6 @@ func (d *SqlDb) GetTemplates(projectID int, params db.RetrieveQueryParams) (temp
case "alias", "playbook":
q = q.Where("pt.project_id=?", projectID).
OrderBy("pt." + params.SortBy + " " + order)
case "ssh_key":
q = q.LeftJoin("access_key ak ON (pt.ssh_key_id = ak.id)").
Where("pt.project_id=?", projectID).
OrderBy("ak.name " + order)
case "inventory":
q = q.LeftJoin("project__inventory pi ON (pt.inventory_id = pi.id)").
Where("pt.project_id=?", projectID).

View File

@ -126,7 +126,7 @@ export default {
try {
item = (await axios({
getRequestOptions: this.isNew ? 'post' : 'put',
method: this.isNew ? 'post' : 'put',
url: this.isNew
? this.getItemsUrl()
: this.getSingleItemUrl(),

View File

@ -52,19 +52,12 @@ export default {
mixins: [ItemFormBase],
data() {
return {
keys: null,
inventoryTypes: [{
id: 'ssh',
name: 'SSH Key',
}, {
id: 'aws',
name: 'AWS IAM credentials',
}, {
id: 'gcloud',
name: 'Google Cloud API Key',
}, {
id: 'do',
name: 'DigitalOcean API Key',
id: 'none',
name: 'None',
}],
};
},

View File

@ -23,17 +23,6 @@
:disabled="formSaving"
></v-text-field>
<v-select
v-model="item.ssh_key_id"
label="SSH Key"
:items="keys"
item-value="id"
item-text="name"
:rules="[v => !!v || 'SSH Key is required']"
required
:disabled="formSaving"
></v-select>
<v-select
v-model="item.inventory_id"
label="Inventory"

View File

@ -131,21 +131,6 @@
</v-list-item>
</v-list>
</v-col>
<v-col>
<v-list two-line subheader>
<v-list-item>
<v-list-item-icon>
<v-icon>mdi-key</v-icon>
</v-list-item-icon>
<v-list-item-content>
<v-list-item-title>SSH Key</v-list-item-title>
<v-list-item-subtitle>
{{ keys.find((x) => x.id === item.ssh_key_id).name }}
</v-list-item-subtitle>
</v-list-item-content>
</v-list-item>
</v-list>
</v-col>
<v-col>
<v-list two-line subheader>
<v-list-item>
@ -240,7 +225,6 @@ export default {
],
tasks: null,
item: null,
keys: null,
inventory: null,
environment: null,
repositories: null,
@ -262,7 +246,6 @@ export default {
isLoaded() {
return this.item
&& this.tasks
&& this.keys
&& this.inventory
&& this.environment
&& this.repositories;
@ -353,12 +336,6 @@ export default {
responseType: 'json',
})).data;
this.keys = (await axios({
method: 'get',
url: `/api/project/${this.projectId}/keys`,
responseType: 'json',
})).data;
this.repositories = (await axios({
method: 'get',
url: `/api/project/${this.projectId}/repositories`,

View File

@ -76,10 +76,6 @@
</router-link>
</template>
<template v-slot:item.ssh_key_id="{ item }">
{{ keys.find((x) => x.id === item.ssh_key_id).name }}
</template>
<template v-slot:item.inventory_id="{ item }">
{{ inventory.find((x) => x.id === item.inventory_id).name }}
</template>
@ -125,7 +121,6 @@ export default {
},
data() {
return {
keys: null,
inventory: null,
environment: null,
repositories: null,
@ -146,7 +141,6 @@ export default {
isLoaded() {
return this.items
&& this.keys
&& this.inventory
&& this.environment
&& this.repositories;
@ -176,11 +170,6 @@ export default {
value: 'playbook',
sortable: false,
},
{
text: 'SSH key',
value: 'ssh_key_id',
sortable: false,
},
{
text: 'Inventory',
value: 'inventory_id',
@ -222,12 +211,6 @@ export default {
responseType: 'json',
})).data;
this.keys = (await axios({
method: 'get',
url: `/api/project/${this.projectId}/keys`,
responseType: 'json',
})).data;
this.repositories = (await axios({
method: 'get',
url: `/api/project/${this.projectId}/repositories`,