Semaphore/api/projects/projects.go

288 lines
5.8 KiB
Go
Raw Normal View History

2016-04-02 14:40:07 +02:00
package projects
import (
"net/http"
"github.com/semaphoreui/semaphore/api/helpers"
"github.com/semaphoreui/semaphore/db"
"github.com/semaphoreui/semaphore/util"
2024-03-10 23:06:17 +01:00
log "github.com/sirupsen/logrus"
"github.com/gorilla/context"
2016-04-02 14:40:07 +02:00
)
// GetProjects returns all projects in this users context
func GetProjects(w http.ResponseWriter, r *http.Request) {
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
if err != nil {
helpers.WriteError(w, err)
return
}
2016-04-02 14:40:07 +02:00
helpers.WriteJSON(w, http.StatusOK, projects)
2016-04-02 14:40:07 +02:00
}
2024-03-25 01:24:04 +01:00
func createDemoProject(projectID int, noneKeyID int, emptyEnvID int, store db.Store) (err error) {
2023-09-17 14:57:57 +02:00
var demoRepo db.Repository
var buildInv db.Inventory
var devInv db.Inventory
var prodInv db.Inventory
2023-09-17 21:55:14 +02:00
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",
2024-03-25 01:24:04 +01:00
SSHKeyID: noneKeyID,
2023-09-17 14:57:57 +02:00
})
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",
2024-03-25 01:24:04 +01:00
SSHKeyID: &noneKeyID,
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",
2024-03-25 01:24:04 +01:00
SSHKeyID: &noneKeyID,
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",
2024-03-25 01:24:04 +01:00
SSHKeyID: &noneKeyID,
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,
InventoryID: &prodInv.ID,
2024-03-25 01:24:04 +01:00
EnvironmentID: &emptyEnvID,
2023-09-17 21:55:14 +02:00
RepositoryID: demoRepo.ID,
App: db.AppAnsible,
2023-09-17 21:55:14 +02:00
})
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,
InventoryID: &buildInv.ID,
2024-03-25 01:24:04 +01:00
EnvironmentID: &emptyEnvID,
2023-09-17 14:57:57 +02:00
RepositoryID: demoRepo.ID,
2023-09-17 21:55:14 +02:00
StartVersion: &startVersion,
App: db.AppAnsible,
2023-09-17 14:57:57 +02:00
})
if err != nil {
return
}
2024-10-03 21:41:36 +02:00
var template db.Template
template, 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,
InventoryID: &devInv.ID,
2024-03-25 01:24:04 +01:00
EnvironmentID: &emptyEnvID,
2023-09-17 21:55:14 +02:00
RepositoryID: demoRepo.ID,
BuildTemplateID: &buildTpl.ID,
Autorun: true,
App: db.AppAnsible,
2023-09-17 14:57:57 +02:00
})
if err != nil {
return
}
2024-10-03 21:41:36 +02:00
_, err = store.CreateTemplateVault(db.TemplateVault{
ProjectID: projectID,
TemplateID: template.ID,
VaultKeyID: &vaultKey.ID,
2024-10-03 21:41:36 +02:00
Name: nil,
Type: "password",
2024-10-03 21:41:36 +02:00
})
if err != nil {
return
}
template, 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,
InventoryID: &prodInv.ID,
2024-03-25 01:24:04 +01:00
EnvironmentID: &emptyEnvID,
2023-09-17 21:55:14 +02:00
RepositoryID: demoRepo.ID,
BuildTemplateID: &buildTpl.ID,
App: db.AppAnsible,
2023-09-17 14:57:57 +02:00
})
2024-10-03 21:41:36 +02:00
if err != nil {
return
}
_, err = store.CreateTemplateVault(db.TemplateVault{
ProjectID: projectID,
TemplateID: template.ID,
VaultKeyID: &vaultKey.ID,
2024-10-03 21:41:36 +02:00
Name: nil,
Type: "password",
2024-10-03 21:41:36 +02:00
})
2023-09-17 14:57:57 +02:00
return
}
// AddProject adds a new project to the database
func AddProject(w http.ResponseWriter, r *http.Request) {
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 {
log.Warn(user.Username + " is not permitted to edit users")
w.WriteHeader(http.StatusUnauthorized)
return
}
2023-09-17 14:57:57 +02:00
var bodyWithDemo struct {
db.Project
Demo bool `json:"demo"`
}
if !helpers.Bind(w, r, &bodyWithDemo) {
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)
if err != nil {
helpers.WriteError(w, err)
return
}
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})
if err != nil {
helpers.WriteError(w, err)
return
}
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-25 01:24:04 +01:00
//_, err = store.CreateInventory(db.Inventory{
// Name: "None",
// ProjectID: body.ID,
// Type: "none",
// SSHKeyID: &noneKey.ID,
//})
if err != nil {
helpers.WriteError(w, err)
return
}
emptyEnv, err := store.CreateEnvironment(db.Environment{
Name: "Empty",
2024-03-10 23:06:17 +01:00
ProjectID: body.ID,
2024-03-25 01:24:04 +01:00
JSON: "{}",
2024-03-10 23:06:17 +01:00
})
if err != nil {
return
}
2023-09-17 14:57:57 +02:00
if bodyWithDemo.Demo {
2024-03-25 01:24:04 +01:00
err = createDemoProject(body.ID, noneKey.ID, emptyEnv.ID, store)
2023-09-17 14:57:57 +02:00
if err != nil {
helpers.WriteError(w, err)
return
}
}
helpers.EventLog(r, helpers.EventLogCreate, helpers.EventLogItem{
UserID: helpers.UserFromContext(r).ID,
ProjectID: body.ID,
ObjectType: db.EventProject,
ObjectID: body.ID,
Description: "Project created",
})
helpers.WriteJSON(w, http.StatusCreated, body)
2016-04-02 14:40:07 +02:00
}