2016-04-02 14:40:07 +02:00
|
|
|
package projects
|
|
|
|
|
|
|
|
import (
|
2020-12-03 14:51:15 +01:00
|
|
|
"github.com/ansible-semaphore/semaphore/api/helpers"
|
2020-12-04 23:41:26 +01:00
|
|
|
"github.com/ansible-semaphore/semaphore/db"
|
2023-07-24 16:04:03 +02:00
|
|
|
"github.com/ansible-semaphore/semaphore/util"
|
2024-03-10 23:06:17 +01:00
|
|
|
log "github.com/sirupsen/logrus"
|
2020-12-01 20:06:49 +01:00
|
|
|
"net/http"
|
2019-07-09 19:45:27 +02:00
|
|
|
|
|
|
|
"github.com/gorilla/context"
|
2016-04-02 14:40:07 +02:00
|
|
|
)
|
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
// GetProjects returns all projects in this users context
|
2019-07-09 18:11:01 +02:00
|
|
|
func GetProjects(w http.ResponseWriter, r *http.Request) {
|
2020-12-04 23:41:26 +01:00
|
|
|
user := context.Get(r, "user").(*db.User)
|
2016-04-02 14:40:07 +02:00
|
|
|
|
2023-09-17 16:15:44 +02:00
|
|
|
var err error
|
|
|
|
var projects []db.Project
|
|
|
|
if user.Admin {
|
|
|
|
projects, err = helpers.Store(r).GetAllProjects()
|
|
|
|
} else {
|
|
|
|
projects, err = helpers.Store(r).GetProjects(user.ID)
|
|
|
|
}
|
2016-04-02 14:40:07 +02:00
|
|
|
|
2020-12-16 20:19:20 +01:00
|
|
|
if err != nil {
|
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
2016-04-02 14:40:07 +02:00
|
|
|
|
2020-12-03 14:51:15 +01:00
|
|
|
helpers.WriteJSON(w, http.StatusOK, projects)
|
2016-04-02 14:40:07 +02:00
|
|
|
}
|
|
|
|
|
2023-09-17 14:57:57 +02:00
|
|
|
func createDemoProject(projectID int, store db.Store) (err error) {
|
|
|
|
var noneKey db.AccessKey
|
|
|
|
var demoRepo db.Repository
|
|
|
|
var emptyEnv db.Environment
|
|
|
|
|
|
|
|
var buildInv db.Inventory
|
|
|
|
var devInv db.Inventory
|
|
|
|
var prodInv db.Inventory
|
|
|
|
|
|
|
|
noneKey, err = store.CreateAccessKey(db.AccessKey{
|
2023-09-17 21:55:14 +02:00
|
|
|
Name: "None",
|
|
|
|
Type: db.AccessKeyNone,
|
|
|
|
ProjectID: &projectID,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-17 22:24:57 +02:00
|
|
|
vaultKey, err := store.CreateAccessKey(db.AccessKey{
|
2023-09-17 21:55:14 +02:00
|
|
|
Name: "Vault Password",
|
|
|
|
Type: db.AccessKeyLoginPassword,
|
|
|
|
ProjectID: &projectID,
|
|
|
|
LoginPassword: db.LoginPassword{
|
|
|
|
Password: "RAX6yKN7sBn2qDagRPls",
|
|
|
|
},
|
2023-09-17 14:57:57 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
demoRepo, err = store.CreateRepository(db.Repository{
|
2023-09-17 21:55:14 +02:00
|
|
|
Name: "Demo",
|
2023-09-17 14:57:57 +02:00
|
|
|
ProjectID: projectID,
|
|
|
|
GitURL: "https://github.com/semaphoreui/demo-project.git",
|
|
|
|
GitBranch: "main",
|
|
|
|
SSHKeyID: noneKey.ID,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
emptyEnv, err = store.CreateEnvironment(db.Environment{
|
|
|
|
Name: "Empty",
|
|
|
|
ProjectID: projectID,
|
|
|
|
JSON: "{}",
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
buildInv, err = store.CreateInventory(db.Inventory{
|
|
|
|
Name: "Build",
|
|
|
|
ProjectID: projectID,
|
2023-09-17 21:55:14 +02:00
|
|
|
Inventory: "[builder]\nlocalhost ansible_connection=local",
|
2023-09-17 14:57:57 +02:00
|
|
|
Type: "static",
|
2023-09-17 21:55:14 +02:00
|
|
|
SSHKeyID: &noneKey.ID,
|
2023-09-17 14:57:57 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
devInv, err = store.CreateInventory(db.Inventory{
|
2023-09-17 21:55:14 +02:00
|
|
|
Name: "Dev",
|
2023-09-17 14:57:57 +02:00
|
|
|
ProjectID: projectID,
|
2024-01-27 16:37:22 +01:00
|
|
|
Inventory: "invs/dev/hosts",
|
2023-09-17 21:55:14 +02:00
|
|
|
Type: "file",
|
2023-09-17 22:24:57 +02:00
|
|
|
SSHKeyID: &noneKey.ID,
|
2023-09-17 14:57:57 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
prodInv, err = store.CreateInventory(db.Inventory{
|
2023-09-17 21:55:14 +02:00
|
|
|
Name: "Prod",
|
2023-09-17 14:57:57 +02:00
|
|
|
ProjectID: projectID,
|
2024-01-27 16:37:22 +01:00
|
|
|
Inventory: "invs/prod/hosts",
|
2023-09-17 21:55:14 +02:00
|
|
|
Type: "file",
|
2023-09-17 22:24:57 +02:00
|
|
|
SSHKeyID: &noneKey.ID,
|
2023-09-17 14:57:57 +02:00
|
|
|
})
|
|
|
|
|
2023-09-17 21:55:14 +02:00
|
|
|
var desc string
|
|
|
|
|
2023-09-17 14:57:57 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-17 21:55:14 +02:00
|
|
|
desc = "This task pings the website to provide real word example of using Semaphore."
|
2023-09-17 14:57:57 +02:00
|
|
|
_, err = store.CreateTemplate(db.Template{
|
2023-09-17 21:55:14 +02:00
|
|
|
Name: "Ping Site",
|
|
|
|
Playbook: "ping.yml",
|
|
|
|
Description: &desc,
|
|
|
|
ProjectID: projectID,
|
2024-03-10 22:56:58 +01:00
|
|
|
InventoryID: prodInv.ID,
|
2023-09-17 21:55:14 +02:00
|
|
|
EnvironmentID: &emptyEnv.ID,
|
|
|
|
RepositoryID: demoRepo.ID,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
desc = "Creates artifact and store it in the cache."
|
|
|
|
|
|
|
|
var startVersion = "1.0.0"
|
|
|
|
buildTpl, err := store.CreateTemplate(db.Template{
|
2023-09-17 14:57:57 +02:00
|
|
|
Name: "Build",
|
|
|
|
Playbook: "build.yml",
|
2023-09-17 21:55:14 +02:00
|
|
|
Type: db.TemplateBuild,
|
2023-09-17 14:57:57 +02:00
|
|
|
ProjectID: projectID,
|
2024-03-10 22:56:58 +01:00
|
|
|
InventoryID: buildInv.ID,
|
2023-09-17 14:57:57 +02:00
|
|
|
EnvironmentID: &emptyEnv.ID,
|
|
|
|
RepositoryID: demoRepo.ID,
|
2023-09-17 21:55:14 +02:00
|
|
|
StartVersion: &startVersion,
|
2023-09-17 14:57:57 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = store.CreateTemplate(db.Template{
|
2023-09-17 21:55:14 +02:00
|
|
|
Name: "Deploy to Dev",
|
|
|
|
Type: db.TemplateDeploy,
|
|
|
|
Playbook: "deploy.yml",
|
|
|
|
ProjectID: projectID,
|
2024-03-10 22:56:58 +01:00
|
|
|
InventoryID: devInv.ID,
|
2023-09-17 21:55:14 +02:00
|
|
|
EnvironmentID: &emptyEnv.ID,
|
|
|
|
RepositoryID: demoRepo.ID,
|
|
|
|
BuildTemplateID: &buildTpl.ID,
|
|
|
|
Autorun: true,
|
2023-09-17 22:24:57 +02:00
|
|
|
VaultKeyID: &vaultKey.ID,
|
2023-09-17 14:57:57 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = store.CreateTemplate(db.Template{
|
2023-09-17 21:55:14 +02:00
|
|
|
Name: "Deploy to Production",
|
|
|
|
Type: db.TemplateDeploy,
|
|
|
|
Playbook: "deploy.yml",
|
|
|
|
ProjectID: projectID,
|
2024-03-10 22:56:58 +01:00
|
|
|
InventoryID: prodInv.ID,
|
2023-09-17 21:55:14 +02:00
|
|
|
EnvironmentID: &emptyEnv.ID,
|
|
|
|
RepositoryID: demoRepo.ID,
|
|
|
|
BuildTemplateID: &buildTpl.ID,
|
2023-09-17 22:24:57 +02:00
|
|
|
VaultKeyID: &vaultKey.ID,
|
2023-09-17 14:57:57 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
// AddProject adds a new project to the database
|
2019-07-09 18:11:01 +02:00
|
|
|
func AddProject(w http.ResponseWriter, r *http.Request) {
|
2020-12-01 20:06:49 +01:00
|
|
|
|
2020-12-04 23:41:26 +01:00
|
|
|
user := context.Get(r, "user").(*db.User)
|
2016-04-02 14:40:07 +02:00
|
|
|
|
2023-07-24 16:04:03 +02:00
|
|
|
if !user.Admin && !util.Config.NonAdminCanCreateProject {
|
2022-01-18 22:50:15 +01:00
|
|
|
log.Warn(user.Username + " is not permitted to edit users")
|
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
2020-12-03 14:51:15 +01:00
|
|
|
|
2023-09-17 14:57:57 +02:00
|
|
|
var bodyWithDemo struct {
|
|
|
|
db.Project
|
|
|
|
Demo bool `json:"demo"`
|
|
|
|
}
|
|
|
|
|
|
|
|
if !helpers.Bind(w, r, &bodyWithDemo) {
|
2019-07-09 18:11:01 +02:00
|
|
|
return
|
|
|
|
}
|
2016-04-02 14:40:07 +02:00
|
|
|
|
2023-09-17 14:57:57 +02:00
|
|
|
body := bodyWithDemo.Project
|
|
|
|
|
|
|
|
store := helpers.Store(r)
|
|
|
|
|
|
|
|
body, err := store.CreateProject(body)
|
2019-07-09 18:11:01 +02:00
|
|
|
if err != nil {
|
2020-12-16 20:19:20 +01:00
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
2016-04-02 14:40:07 +02:00
|
|
|
|
2023-09-17 14:57:57 +02:00
|
|
|
_, err = store.CreateProjectUser(db.ProjectUser{ProjectID: body.ID, UserID: user.ID, Role: db.ProjectOwner})
|
2020-12-01 20:06:49 +01:00
|
|
|
if err != nil {
|
2020-12-16 20:19:20 +01:00
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
2016-04-02 14:40:07 +02:00
|
|
|
|
2024-03-11 02:22:17 +01:00
|
|
|
noneKey, err := store.CreateAccessKey(db.AccessKey{
|
|
|
|
Name: "None",
|
|
|
|
Type: db.AccessKeyNone,
|
|
|
|
ProjectID: &body.ID,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-10 23:06:17 +01:00
|
|
|
_, err = store.CreateInventory(db.Inventory{
|
|
|
|
Name: "None",
|
|
|
|
ProjectID: body.ID,
|
|
|
|
Type: "none",
|
2024-03-11 02:22:17 +01:00
|
|
|
SSHKeyID: &noneKey.ID,
|
2024-03-10 23:06:17 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-17 14:57:57 +02:00
|
|
|
if bodyWithDemo.Demo {
|
|
|
|
err = createDemoProject(body.ID, store)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-09 18:11:01 +02:00
|
|
|
desc := "Project Created"
|
2021-10-13 16:07:22 +02:00
|
|
|
oType := db.EventProject
|
2023-09-17 14:57:57 +02:00
|
|
|
_, err = store.CreateEvent(db.Event{
|
2021-08-20 08:28:50 +02:00
|
|
|
UserID: &user.ID,
|
2019-07-09 18:11:01 +02:00
|
|
|
ProjectID: &body.ID,
|
|
|
|
Description: &desc,
|
|
|
|
ObjectType: &oType,
|
|
|
|
ObjectID: &body.ID,
|
2020-12-01 20:06:49 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
2016-04-17 02:20:23 +02:00
|
|
|
|
2020-12-03 14:51:15 +01:00
|
|
|
helpers.WriteJSON(w, http.StatusCreated, body)
|
2016-04-02 14:40:07 +02:00
|
|
|
}
|