123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371 |
- package semaphore
- import (
- "bytes"
- "encoding/json"
- "fmt"
- "io"
- "net/http"
- "strconv"
- "strings"
- "time"
- "git.aetherial.dev/aeth/yosai/pkg/daemon"
- "git.aetherial.dev/aeth/yosai/pkg/keytags"
- "gopkg.in/yaml.v3"
- )
- const ProjectsPath = "api/projects"
- const ProjectPath = "api/project"
- const YosaiProject = "Yosai VPN Sentinel"
- const YosaiServerInventory = "Yosai VPN Servers"
- const YosaiVpnRotationJob = "VPN Rotation playbook"
- const YosaiEnvironment = "VPN Server configuration environment variables"
- type SemaphoreConnection struct {
- Client *http.Client
- Keyring daemon.DaemonKeyRing
- KeyTagger keytags.Keytagger
- Config daemon.Configuration
- ServerUrl string
- HttpProto string
- ProjectId int
- }
- type TaskInfo struct {
- ID int `json:"id"`
- TemplateID int `json:"template_id"`
- Status string `json:"status"`
- Debug bool `json:"debug"`
- Playbook string `json:"playbook"`
- Environment string `json:"environment"`
- Secret string `json:"secret"`
- Limit string `json:"limit"`
- }
- type TaskOutput struct {
- TaskID int `json:"task_id"`
- Task string `json:"task"`
- Time time.Time `json:"time"`
- Output string `json:"output"`
- }
- type NewTemplateRequest struct {
- ProjectId int `json:"project_id"`
- Name string `json:"name"`
- InventoryId int `json:"inventory_id"`
- RepositoryId int `json:"repository_id"`
- EnvironmentId int `json:"environment_id"`
- Playbook string `json:"playbook"`
- Type string `json:"type"`
- }
- type JobTemplate struct {
- Id int `json:"id"`
- ProjectId int `json:"project_id"`
- Name string `json:"name"`
- InventoryId int `json:"inventory_id"`
- RepositoryId int `json:"repository_id"`
- EnvironmentId int `json:"environment_id"`
- Playbook string `json:"playbook"`
- }
- type StartTaskRequest struct {
- TemplateID int `json:"template_id"`
- ProjectId int `json:"project_id"`
- }
- type StartTaskResponse struct {
- Id int `json:"id"`
- TemplateID int `json:"template_id"`
- Debug bool `json:"debug"`
- DryRun bool `json:"dry_run"`
- Diff bool `json:"diff"`
- Playbook string `json:"playbook"`
- Environment string `json:"environment"`
- Limit string `json:"limit"`
- }
- type EnvironmentVariables struct {
- SecretsProviderUrl string `json:"SECRETS_PROVIDER_URL"`
- SecretsProviderApiKey string `json:"SECRETS_PROVIDER_API_KEY"`
- }
- type AddEnvironmentRequest struct {
- Name string `json:"name"`
- ProjectID int `json:"project_id"`
- Password string `json:"password"`
- JSON string `json:"json"`
- Env string `json:"env"`
- }
- type EnvironmentResponse struct {
- Id int `json:"id"`
- Name string `json:"name"`
- ProjectID int `json:"project_id"`
- Password string `json:"password"`
- JSON string `json:"json"`
- Env string `json:"env"`
- }
- type ProjectsResponse struct {
- Id int `json:"id"`
- Name string `json:"name"`
- Created string `json:"created"`
- Alert bool `json:"alert"`
- AlertChat string `json:"alert_chat"`
- MaxParallelTasks int `json:"max_parallel_tasks"`
- }
- type NewProjectReqeust struct {
- Name string `json:"name"`
- Alert bool `json:"alert"`
- AlertChat string `json:"alert_chat"`
- MaxParallelTasks int `json:"max_parallel_tasks"`
- }
- type NewRepoRequest struct {
- Name string `json:"name"` // name of the project
- ProjectId int `json:"project_id"` // the numerical ID of the project as per /api/project/<project id>
- GitUrl string `json:"git_url"` // the URL of the git repo (SSH address)
- GitBranch string `json:"git_branch"` // the branch to pull down
- SshKeyId int `json:"ssh_key_id"` // the numerical ID of the ssh key for the repository, as per /api/project/<project id>/keys
- }
- type NewRepoResponse struct {
- Id int `json:"id"` // the numerical ID assigned to the repo by Semaphore
- Name string `json:"name"` // name of the project
- ProjectId int `json:"project_id"` // the numerical ID of the project as per /api/project/<project id>
- GitUrl string `json:"git_url"` // the URL of the git repo (SSH address)
- GitBranch string `json:"git_branch"` // the branch to pull down
- SshKeyId int `json:"ssh_key_id"` // the numerical ID of the ssh key for the repository, as per /api/project/<project id>/keys
- }
- type AddKeyRequest struct {
- Name string `json:"name"`
- Type string `json:"type"`
- ProjectId int `json:"project_id"`
- LoginPassword loginPassword `json:"login_password"`
- Ssh sshKeyAdd `json:"ssh"`
- }
- func (a AddKeyRequest) GetPublic() string {
- if a.Type == "ssh" {
- return a.Ssh.Login
- } else {
- return a.LoginPassword.Login
- }
- }
- func (a AddKeyRequest) GetSecret() string {
- if a.Type == "ssh" {
- return a.Ssh.PrivateKey
- } else {
- return a.LoginPassword.Password
- }
- }
- func (a AddKeyRequest) Prepare() string {
- b, err := json.Marshal(a)
- if err != nil {
- return err.Error()
- }
- return string(b)
- }
- func (a AddKeyRequest) GetType() string {
- return a.Type
- }
- type KeyItemResponse struct {
- Id int `json:"id"`
- Name string `json:"name"`
- Type string `json:"type"`
- ProjectId int `json:"project_id"`
- LoginPassword loginPassword `json:"login_password"`
- Ssh sshKeyAdd `json:"ssh"`
- }
- type NewInventoryRequest struct {
- Name string `json:"name"`
- ProjectId int `json:"project_id"`
- Inventory string `json:"inventory"` // This field is where the YAML inventory file gets put, as a string (not a filepath!)
- Type string `json:"type"`
- SshKeyId int `json:"ssh_key_id"`
- BecomeKeyId int `json:"become_key_id"`
- }
- type InventoryResponse struct {
- Id int `json:"id"`
- Inventory string `json:"inventory"`
- Name string `json:"name"`
- ProjectId int `json:"project_id"`
- Type string `json:"type"`
- SshKeyId int `json:"ssh_key_id"`
- BecomeKeyId int `json:"become_key_id"`
- }
- /*
- ####################################################################
- ############ IMPLEMENTING daemon.Key FOR KeyItemResponse ###########
- ####################################################################
- */
- func (k KeyItemResponse) GetPublic() string {
- return k.Ssh.Login
- }
- func (k KeyItemResponse) GetSecret() string {
- return k.Ssh.PrivateKey
- }
- func (k KeyItemResponse) Prepare() string {
- return k.Type
- }
- func (k KeyItemResponse) GetType() string {
- return k.Type
- }
- type loginPassword struct {
- Password string `json:"password"`
- Login string `json:"login"`
- }
- type sshKeyAdd struct {
- PrivateKey string `json:"private_key"`
- Login string `json:"login"`
- }
- /*
- ###################################################################
- ########### IMPLEMENTING THE DaemonKeyRing INTERFACE ##############
- ###################################################################
- */
- /*
- Get SSH key by its name
- */
- func (s SemaphoreConnection) GetKey(name string) (daemon.Key, error) {
- var key KeyItemResponse
- keys, err := s.GetAllKeys()
- if err != nil {
- return key, daemon.KeyRingError
- }
- for i := range keys {
- if keys[i].Name == name {
- return keys[i], nil
- }
- }
- return key, daemon.KeyNotFound
- }
- /*
- Add SSH Key to a project
- :param name: the name to assign the key in the project
- :param keyring: a daemon.DaemonKeyRing implementer that can return the API key for Semaphore
- :param key: a daemon.Key implementer wrapping the SSH key
- */
- func (s SemaphoreConnection) AddKey(name string, key daemon.Key) error {
- _, err := s.GetKeyId(name)
- if err == nil { // return if the key exists
- return nil
- }
- path := fmt.Sprintf("%s/%v/keys", ProjectPath, s.ProjectId)
- _, err = s.Post(path, bytes.NewReader([]byte(key.Prepare())))
- if err != nil {
- return err
- }
- return nil
- }
- /*
- Drop a key from the Semaphore secret store
- */
- func (s SemaphoreConnection) RemoveKey(name string) error {
- _, err := s.Delete(name)
- return err
- }
- // Return the resource name for logging purposes
- func (s SemaphoreConnection) Source() string {
- return "Semaphore Keystore"
- }
- // NewKeyRequest builder function
- func (s SemaphoreConnection) NewKeyRequestBuilder(name string, key daemon.Key) daemon.Key {
- if key.GetType() == "ssh" {
- return AddKeyRequest{
- Name: name,
- Type: key.GetType(),
- ProjectId: s.ProjectId,
- Ssh: sshKeyAdd{
- Login: key.GetPublic(),
- PrivateKey: key.GetSecret(),
- },
- }
- } else {
- return AddKeyRequest{
- Name: name,
- Type: key.GetType(),
- ProjectId: s.ProjectId,
- LoginPassword: loginPassword{
- Login: key.GetPublic(),
- Password: key.GetSecret(),
- },
- }
- }
- }
- /*
- Create a new semaphore client
- :param url: the base url of the semaphore server, without the HTTP/S prefix
- :param proto: either HTTP or HTTPS, depending on the server's SSL setup
- :param log: an io.Writer to write logfile to
- :param keyring: a daemon.DaemonKeyRing implementer to get the Semaphore API key from
- */
- func NewSemaphoreClient(url string, proto string, log io.Writer, keyring daemon.DaemonKeyRing, conf daemon.Configuration, keytagger keytags.Keytagger) SemaphoreConnection {
- log.Write([]byte("Using HTTP mode: " + proto + "\n"))
- client := &http.Client{}
- semaphoreBootstrap := SemaphoreConnection{Client: client, ServerUrl: url, HttpProto: proto, Keyring: keyring, KeyTagger: keytagger}
- id, err := semaphoreBootstrap.GetProjectByName(YosaiProject)
- if err != nil {
- log.Write([]byte(YosaiProject + " NOT FOUND IN SEMAPHORE. Creating..."))
- err = semaphoreBootstrap.NewProject(YosaiProject)
- if err != nil {
- log.Write([]byte("FATAL ERROR CREATING PROJECT. ABANDONING SHIP. Error: " + err.Error()))
- }
- id, _ = semaphoreBootstrap.GetProjectByName(YosaiProject)
- log.Write([]byte("Found " + YosaiProject + " with project id: " + fmt.Sprint(id)))
- return SemaphoreConnection{
- Client: client,
- ServerUrl: url,
- HttpProto: proto,
- ProjectId: id,
- Keyring: keyring,
- Config: conf,
- KeyTagger: keytagger,
- }
- }
- return SemaphoreConnection{
- Client: &http.Client{},
- ServerUrl: url,
- HttpProto: proto,
- ProjectId: id,
- Keyring: keyring,
- Config: conf,
- KeyTagger: keytagger,
- }
- }
- /*
- Create a new 'Project' in Semaphore
- :param name: the name to assign the project
- :param keyring: a daemon.DaemonKeyRing implementer to get the Semaphore API key from
- */
- func (s SemaphoreConnection) NewProject(name string) error {
- _, err := s.GetProjectByName(name)
- if err == nil {
- return nil // return nil of project already exists
- }
- var b []byte
- newProj := NewProjectReqeust{
- Name: name,
- Alert: false,
- AlertChat: "",
- MaxParallelTasks: 0,
- }
- b, err = json.Marshal(&newProj)
- if err != nil {
- return &SemaphoreClientError{Msg: err.Error()}
- }
- _, err = s.Post(ProjectsPath, bytes.NewReader(b))
- if err != nil {
- return err
- }
- return nil
- }
- /*
- Add a repository to the project designated for the Yosai service
- :param giturl: the url for the git repo containing the ansible scripts for VPN server config
- :param branch: the branch to target on the git repo
- */
- func (s SemaphoreConnection) AddRepository(giturl string, branch string) error {
- _, err := s.GetRepoByName(fmt.Sprintf("%s:%s", giturl, branch))
- if err == nil { // return if the repo exists
- return nil
- }
- sshKeyId, err := s.GetKeyId(s.KeyTagger.GitSshKeyname())
- if err != nil {
- return err
- }
- repoAddRequest := NewRepoRequest{
- Name: fmt.Sprintf("%s:%s", giturl, branch),
- ProjectId: s.ProjectId,
- GitUrl: giturl,
- GitBranch: branch,
- SshKeyId: sshKeyId,
- }
- b, err := json.Marshal(&repoAddRequest)
- if err != nil {
- return &SemaphoreClientError{Msg: err.Error()}
- }
- _, err = s.Post(fmt.Sprintf("%s/%v/repositories", ProjectPath, s.ProjectId), bytes.NewReader(b))
- if err != nil {
- return err
- }
- return nil
- }
- /*
- Generic POST Request to sent to the Semaphore server
- :param path: the path to the API to POST. Preceeding slashes will be trimmed
- :param body: an io.Reader implementer to use as the POST body. Must comply with application/json Content-Type
- */
- func (s SemaphoreConnection) Put(path string, body io.Reader) ([]byte, error) {
- var b []byte
- apikey, err := s.Keyring.GetKey(s.KeyTagger.SemaphoreApiKeyname())
- if err != nil {
- return b, &SemaphoreClientError{Msg: err.Error()}
- }
- req, err := http.NewRequest(http.MethodPut, fmt.Sprintf("%s://%s/%s", s.HttpProto, s.ServerUrl, strings.TrimPrefix(path, "/")), body)
- if err != nil {
- return b, &SemaphoreClientError{Msg: err.Error()}
- }
- req.Header.Add("Authorization", apikey.Prepare())
- req.Header.Add("Content-Type", "application/json")
- resp, err := s.Client.Do(req)
- if err != nil {
- return b, &SemaphoreClientError{Msg: err.Error()}
- }
- defer resp.Body.Close()
- if resp.StatusCode >= 400 {
- return b, &SemaphoreClientError{Msg: resp.Status}
- }
- b, err = io.ReadAll(resp.Body)
- if err != nil {
- return b, &SemaphoreClientError{Msg: err.Error()}
- }
- return b, nil
- }
- /*
- Generic POST Request to sent to the Semaphore server
- :param path: the path to the API to POST. Preceeding slashes will be trimmed
- :param body: an io.Reader implementer to use as the POST body. Must comply with application/json Content-Type
- */
- func (s SemaphoreConnection) Post(path string, body io.Reader) ([]byte, error) {
- var b []byte
- apikey, err := s.Keyring.GetKey(s.KeyTagger.SemaphoreApiKeyname())
- if err != nil {
- return b, &SemaphoreClientError{Msg: err.Error()}
- }
- req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s://%s/%s", s.HttpProto, s.ServerUrl, strings.TrimPrefix(path, "/")), body)
- if err != nil {
- return b, &SemaphoreClientError{Msg: err.Error()}
- }
- req.Header.Add("Authorization", apikey.Prepare())
- req.Header.Add("Content-Type", "application/json")
- resp, err := s.Client.Do(req)
- if err != nil {
- return b, &SemaphoreClientError{Msg: err.Error()}
- }
- defer resp.Body.Close()
- b, err = io.ReadAll(resp.Body)
- if err != nil {
- return b, &SemaphoreClientError{Msg: err.Error()}
- }
- if resp.StatusCode >= 400 {
- return b, &SemaphoreClientError{Msg: resp.Status}
- }
- return b, nil
- }
- /*
- Agnostic GET method for calling the upstream Semaphore server
- :param path: the path to GET, added into the base API url
- */
- func (s SemaphoreConnection) Get(path string) ([]byte, error) {
- var b []byte
- apiKey, err := s.Keyring.GetKey(s.KeyTagger.SemaphoreApiKeyname())
- if err != nil {
- return b, &SemaphoreClientError{Msg: err.Error()}
- }
- req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s://%s/%s", s.HttpProto, s.ServerUrl, strings.TrimPrefix(path, "/")), nil)
- if err != nil {
- return b, &SemaphoreClientError{Msg: err.Error()}
- }
- req.Header.Add("Authorization", apiKey.Prepare())
- resp, err := s.Client.Do(req)
- if err != nil {
- return b, &SemaphoreClientError{Msg: err.Error()}
- }
- defer resp.Body.Close()
- b, err = io.ReadAll(resp.Body)
- if err != nil {
- return b, &SemaphoreClientError{Msg: err.Error()}
- }
- return b, nil
- }
- /*
- Generic DELETE method for calling the Semaphore server
- */
- func (s SemaphoreConnection) Delete(path string) ([]byte, error) {
- return []byte{}, nil
- }
- /*
- Retrieve the projects in Semaphore
- :param keyring: a daemon.DaemonKeyRing implementer to get the API key from for Semaphore
- */
- func (s SemaphoreConnection) GetProjects() ([]ProjectsResponse, error) {
- var projectsResp []ProjectsResponse
- b, err := s.Get(ProjectsPath)
- if err != nil {
- return projectsResp, err
- }
- err = json.Unmarshal(b, &projectsResp)
- if err != nil {
- return projectsResp, &SemaphoreClientError{Msg: err.Error()}
- }
- return projectsResp, nil
- }
- /*
- Get Project by its name, and return its ID
- */
- func (s SemaphoreConnection) GetProjectByName(name string) (int, error) {
- projects, err := s.GetProjects()
- if err != nil {
- return 0, err
- }
- for i := range projects {
- if projects[i].Name == name {
- return projects[i].Id, nil
- }
- }
- return 0, &SemaphoreClientError{Msg: fmt.Sprintf("Project with name: '%s' not found.", name)}
- }
- /*
- Get SSH Keys from the current project
- */
- func (s SemaphoreConnection) GetAllKeys() ([]KeyItemResponse, error) {
- var keys []KeyItemResponse
- b, err := s.Get(fmt.Sprintf("%s/%v/keys", ProjectPath, s.ProjectId))
- if err != nil {
- return keys, err
- }
- err = json.Unmarshal(b, &keys)
- if err != nil {
- return keys, &SemaphoreClientError{Msg: err.Error()}
- }
- return keys, nil
- }
- /*
- Return a key ID from the Semaphore keystore by it's name
- :param keyname: the name of the key in Semaphore
- */
- func (s SemaphoreConnection) GetKeyId(keyname string) (int, error) {
- keys, err := s.GetAllKeys()
- if err != nil {
- return 0, err
- }
- for i := range keys {
- if keys[i].Name == keyname {
- return keys[i].Id, nil
- }
- }
- return 0, &KeyNotFound{Keyname: keyname}
- }
- /*
- Get the output of a task
- :param taskId: the ID of the task that was ran
- */
- func (s SemaphoreConnection) GetTaskOutput(taskId int) ([]TaskOutput, error) {
- var taskout []TaskOutput
- b, err := s.Get(fmt.Sprintf("%s/%v/tasks/%v/output", ProjectPath, s.ProjectId, taskId))
- if err != nil {
- return taskout, err
- }
- err = json.Unmarshal(b, &taskout)
- if err != nil {
- return taskout, &SemaphoreClientError{Msg: "Could not unmarshall the response from getting task output." + err.Error()}
- }
- return taskout, nil
- }
- /*
- Get information relating to a task
- :param taskId: the ID of the task that was ran
- */
- func (s SemaphoreConnection) GetTaskInfo(taskId int) (TaskInfo, error) {
- var taskout TaskInfo
- b, err := s.Get(fmt.Sprintf("%s/%v/tasks/%v", ProjectPath, s.ProjectId, taskId))
- if err != nil {
- return taskout, err
- }
- err = json.Unmarshal(b, &taskout)
- if err != nil {
- return taskout, &SemaphoreClientError{Msg: "Could not unmarshall the response from getting task output." + err.Error()}
- }
- return taskout, nil
- }
- /*
- Poll for task completion
- :param taskId: the ID of the task to be polled
- :param max_tries: the number of times to poll the running task before timing out
- */
- func (s SemaphoreConnection) PollTask(taskId int, max_tries int) error {
- var attempts int
- for {
- attempts = attempts + 1
- s.Config.Log("Polling task: ", fmt.Sprint(taskId), " for ", fmt.Sprint(attempts), " times.")
- if attempts > max_tries {
- s.Config.Log("Polling for job completion timed out after: ", fmt.Sprint(attempts), " attempts.")
- return &SemaphoreTimeout{Tries: attempts}
- }
- resp, err := s.GetTaskInfo(taskId)
- if err != nil {
- return err
- }
- s.Config.Log("Job: ", fmt.Sprint(taskId), " is marked with status: ", resp.Status)
- if resp.Status == "success" {
- return nil
- }
- if resp.Status == "error" {
- return &SemaphoreTimeout{Tries: attempts}
- }
- time.Sleep(time.Second * 5)
- }
- }
- /*
- Add an inventory to semaphore
- :param hosts: a list of IP addresses to add to the inventory
- */
- func (s SemaphoreConnection) AddInventory(name string, hosts ...string) error {
- _, err := s.GetInventoryByName(name)
- if err == nil { // Returning on nil error because that means the inventory exists
- return &SemaphoreClientError{Msg: "Inventory Exists! Please update rather than create a new."}
- }
- sshKeyId, err := s.GetKeyId(s.KeyTagger.VpsSvcAccSshPubkeySeed())
- if err != nil {
- return err
- }
- becomeKeyId, err := s.GetKeyId(s.KeyTagger.VpsSvcAccKeyname())
- if err != nil {
- return err
- }
- pubkey, err := s.Keyring.GetKey(s.KeyTagger.WgClientKeypairKeyname())
- if err != nil {
- return &SemaphoreClientError{Msg: err.Error() + s.KeyTagger.WgClientKeypairKeyname()}
- }
- inv := s.YamlInventoryBuilder(hosts, pubkey.GetPublic())
- b, err := yaml.Marshal(inv)
- if err != nil {
- return &SemaphoreClientError{Msg: err.Error()}
- }
- body := NewInventoryRequest{
- Name: name,
- ProjectId: s.ProjectId,
- Inventory: string(b),
- SshKeyId: sshKeyId,
- BecomeKeyId: becomeKeyId,
- Type: "static-yaml",
- }
- requestBody, err := json.Marshal(&body)
- if err != nil {
- return &SemaphoreClientError{Msg: err.Error()}
- }
- _, err = s.Post(fmt.Sprintf("%s/%v/%s", ProjectPath, s.ProjectId, "inventory"), bytes.NewReader(requestBody))
- return err
- }
- /*
- Get Inventory by name and return its ID
- :param name: the name of the inventory to find
- */
- func (s SemaphoreConnection) GetInventoryByName(name string) (InventoryResponse, error) {
- var out InventoryResponse
- resp, err := s.GetAllInventories()
- if err != nil {
- return out, err
- }
- for i := range resp {
- if resp[i].Name == name {
- return resp[i], nil
- }
- }
- return out, &KeyNotFound{Keyname: name}
- }
- /*
- Get all inventories from Semaphore
- */
- func (s SemaphoreConnection) GetAllInventories() ([]InventoryResponse, error) {
- var resp []InventoryResponse
- b, err := s.Get(fmt.Sprintf("%s/%v/%s", ProjectPath, s.ProjectId, "inventory"))
- if err != nil {
- return resp, err
- }
- err = json.Unmarshal(b, &resp)
- if err != nil {
- return resp, &SemaphoreClientError{Msg: err.Error()}
- }
- return resp, nil
- }
- /*
- Update an inventory
- */
- func (s SemaphoreConnection) UpdateInventory(name string, inv YamlInventory) error {
- sshKeyId, err := s.GetKeyId(s.KeyTagger.VpsSvcAccSshPubkeySeed())
- if err != nil {
- return err
- }
- becomeKeyId, err := s.GetKeyId(s.KeyTagger.VpsSvcAccKeyname())
- if err != nil {
- return err
- }
- b, err := yaml.Marshal(inv)
- if err != nil {
- return &SemaphoreClientError{Msg: "Error unmarshalling YAML inventory payload: " + err.Error()}
- }
- targetInv, err := s.GetInventoryByName(name)
- if err != nil {
- return &SemaphoreClientError{Msg: "Target inventory: " + name + " was not found."}
- }
- body := InventoryResponse{
- Id: targetInv.Id,
- Name: name,
- ProjectId: s.ProjectId,
- Inventory: string(b),
- SshKeyId: sshKeyId,
- BecomeKeyId: becomeKeyId,
- Type: "static-yaml",
- }
- req, err := json.Marshal(body)
- if err != nil {
- return &SemaphoreClientError{Msg: "There was an error marshalling the JSON payload: " + err.Error()}
- }
- _, err = s.Put(fmt.Sprintf("%s/%v/inventory/%v", ProjectPath, s.ProjectId, targetInv.Id), bytes.NewReader(req))
- return err
- }
- /*
- Remove host from an inventory
- */
- func (s SemaphoreConnection) RemoveHostFromInv(name string, host ...string) error {
- resp, err := s.GetInventoryByName(name)
- if err != nil {
- return err
- }
- var inv YamlInventory
- err = yaml.Unmarshal([]byte(resp.Inventory), &inv)
- if err != nil {
- return &SemaphoreClientError{Msg: "Error unmarshalling inventory from server: " + resp.Inventory + err.Error()}
- }
- for i := range host {
- _, ok := inv.All.Hosts[host[i]]
- if !ok {
- return &SemaphoreClientError{Msg: "Host: " + host[i] + " not found in the inventory: " + resp.Inventory}
- }
- delete(inv.All.Hosts, host[i])
- }
- pubkey, err := s.Keyring.GetKey(s.KeyTagger.WgClientKeypairKeyname())
- if err != nil {
- return &SemaphoreClientError{Msg: err.Error() + s.KeyTagger.WgClientKeypairKeyname()}
- }
- var hosts []string
- for k := range inv.All.Hosts {
- hosts = append(hosts, k)
- }
- return s.UpdateInventory(name, s.YamlInventoryBuilder(hosts, pubkey.GetPublic()))
- }
- /*
- Add hosts to inventory
- */
- func (s SemaphoreConnection) AddHostToInv(name string, host ...string) error {
- resp, err := s.GetInventoryByName(name)
- if err != nil {
- return err
- }
- var inv YamlInventory
- err = yaml.Unmarshal([]byte(resp.Inventory), &inv)
- if err != nil {
- return &SemaphoreClientError{Msg: "Error unmarshalling inventory from server: " + resp.Inventory + err.Error()}
- }
- pubkey, err := s.Keyring.GetKey(s.KeyTagger.WgClientKeypairKeyname())
- if err != nil {
- return &SemaphoreClientError{Msg: err.Error() + s.KeyTagger.WgClientKeypairKeyname()}
- }
- var hosts []string
- for k := range inv.All.Hosts {
- hosts = append(hosts, k)
- }
- hosts = append(hosts, host...)
- return s.UpdateInventory(name, s.YamlInventoryBuilder(hosts, pubkey.GetPublic()))
- }
- /*
- Get a repo ID by its name
- :param name: the name of the repo
- */
- func (s SemaphoreConnection) GetRepoByName(name string) (int, error) {
- resp, err := s.GetAllRepos()
- if err != nil {
- return 0, err
- }
- for i := range resp {
- if resp[i].Name == name {
- return resp[i].Id, nil
- }
- }
- return 0, &KeyNotFound{Keyname: name}
- }
- /*
- Get all repositories from Semaphore
- */
- func (s SemaphoreConnection) GetAllRepos() ([]NewRepoResponse, error) {
- var resp []NewRepoResponse
- b, err := s.Get(fmt.Sprintf("%s/%v/%s", ProjectPath, s.ProjectId, "repositories"))
- if err != nil {
- return resp, &SemaphoreClientError{Msg: err.Error()}
- }
- err = json.Unmarshal(b, &resp)
- if err != nil {
- return resp, &SemaphoreClientError{Msg: err.Error()}
- }
- return resp, nil
- }
- // Create an environment variable configuration, currently unimplemented
- func (s SemaphoreConnection) AddEnvironment(envVars EnvironmentVariables) error {
- envBytes, err := json.Marshal(envVars)
- if err != nil {
- return &SemaphoreClientError{Msg: "Couldnt unmarshall the environment variable payload: " + err.Error()}
- }
- _, err = s.GetEnvironmentId(YosaiEnvironment)
- if err == nil {
- return nil // environment exists, dont add another with same name
- }
- var body AddEnvironmentRequest
- body = AddEnvironmentRequest{
- Name: YosaiEnvironment,
- ProjectID: s.ProjectId,
- JSON: "{}",
- Env: string(envBytes),
- }
- b, err := json.Marshal(body)
- if err != nil {
- return &SemaphoreClientError{Msg: "couldnt marshal the JSON payload"}
- }
- _, err = s.Post(fmt.Sprintf("%s/%v/environment", ProjectPath, s.ProjectId), bytes.NewBuffer(b))
- return err
- }
- // Get an environment configuration ID by name.
- func (s SemaphoreConnection) GetEnvironmentId(name string) (int, error) {
- var env []EnvironmentResponse
- b, err := s.Get(fmt.Sprintf("%s/%v/environment", ProjectPath, s.ProjectId))
- if err != nil {
- return 0, err
- }
- err = json.Unmarshal(b, &env)
- if err != nil {
- return 0, &SemaphoreClientError{Msg: "Couldnt unmarshall the response"}
- }
- for i := range env {
- if env[i].Name == name {
- return env[i].Id, nil
- }
- }
- return 0, &KeyNotFound{Keyname: "Couldnt find environment: " + name}
- }
- /*
- Add job template to the Yosai project on Semaphore
- :param playbook: the name of the playbook file
- :param repoName: the name of the repo that the playbook belongs to
- */
- func (s SemaphoreConnection) AddJobTemplate(playbook string, repoName string) error {
- _, err := s.JobTemplateByName(YosaiVpnRotationJob)
- if err == nil {
- return nil // return nil because template exists
- }
- repoId, err := s.GetRepoByName(repoName)
- if err != nil {
- return err
- }
- InventoryItem, err := s.GetInventoryByName(YosaiServerInventory)
- if err != nil {
- return err
- }
- envId, err := s.GetEnvironmentId(YosaiEnvironment)
- if err != nil {
- return err
- }
- templ := NewTemplateRequest{
- ProjectId: s.ProjectId,
- Name: YosaiVpnRotationJob,
- InventoryId: InventoryItem.Id,
- RepositoryId: repoId,
- EnvironmentId: envId,
- Playbook: playbook,
- Type: "",
- }
- b, err := json.Marshal(templ)
- if err != nil {
- return &SemaphoreClientError{Msg: err.Error()}
- }
- b, err = s.Post(fmt.Sprintf("%s/%v/%s", ProjectPath, s.ProjectId, "templates"), bytes.NewReader(b))
- if err != nil {
- return &SemaphoreClientError{Msg: fmt.Sprintf("Error: %s\nServer Response: %s", err.Error(), string(b))}
- }
- return nil
- }
- /*
- Start a task in Semaphore by the template name
- :param name: the name of the job template to start
- */
- func (s SemaphoreConnection) StartJob(name string) (StartTaskResponse, error) {
- var resp StartTaskResponse
- template, err := s.JobTemplateByName(name)
- if err != nil {
- return resp, &SemaphoreClientError{Msg: "Could not start job template: " + name + "Error: " + err.Error()}
- }
- var jobReq StartTaskRequest
- jobReq = StartTaskRequest{
- TemplateID: template.Id,
- ProjectId: s.ProjectId,
- }
- b, err := json.Marshal(&jobReq)
- if err != nil {
- return resp, &SemaphoreClientError{Msg: "Couldnt marshal data into byte array: " + err.Error()}
- }
- rb, err := s.Post(fmt.Sprintf("%s/%v/tasks", ProjectPath, s.ProjectId), bytes.NewReader(b))
- if err != nil {
- return resp, err
- }
- err = json.Unmarshal(rb, &resp)
- if err != nil {
- return resp, &SemaphoreClientError{Msg: "Couldnt unmarshal the response from semaphore: " + err.Error()}
- }
- return resp, nil
- }
- /*
- Get a job template ID by name
- :param name: the name of the job template ID
- */
- func (s SemaphoreConnection) GetAllTemplates() ([]JobTemplate, error) {
- var jobs []JobTemplate
- resp, err := s.Get(fmt.Sprintf("%s/%v/templates", ProjectPath, s.ProjectId))
- if err != nil {
- return jobs, err
- }
- err = json.Unmarshal(resp, &jobs)
- if err != nil {
- return jobs, &SemaphoreClientError{Msg: "Error unmarshalling payload response: " + err.Error()}
- }
- return jobs, nil
- }
- /*
- Bootstrap the Semaphore environment
- */
- /*
- Get a job template ID by name
- :param name: the name of the job template ID
- */
- func (s SemaphoreConnection) JobTemplateByName(name string) (JobTemplate, error) {
- var job JobTemplate
- jobs, err := s.GetAllTemplates()
- if err != nil {
- return job, err
- }
- for i := range jobs {
- if jobs[i].Name == name {
- return jobs[i], nil
- }
- }
- return job, &SemaphoreClientError{Msg: "Job with name" + name + "not found"}
- }
- /*
- ##########################################################
- ################## DAEMON ROUTE HANDLERS #################
- ##########################################################
- */
- type SemaphoreRequest struct {
- Target string `json:"target"`
- }
- /*
- Wrapping the functioanlity of the keyring bootstrapper for top level cleanliness
- */
- func (s SemaphoreConnection) keyBootstrapper() daemon.SockMessage {
- reqKeys := s.KeyTagger.GetAnsibleKeys()
- for i := range reqKeys {
- kn := reqKeys[i]
- key, err := s.Keyring.GetKey(kn)
- if err != nil {
- return *daemon.NewSockMessage(daemon.MsgResponse, daemon.REQUEST_FAILED, []byte(err.Error()))
- }
- err = s.AddKey(kn, s.NewKeyRequestBuilder(kn, key))
- if err != nil {
- return *daemon.NewSockMessage(daemon.MsgResponse, daemon.REQUEST_FAILED, []byte(err.Error()))
- }
- }
- return *daemon.NewSockMessage(daemon.MsgResponse, daemon.REQUEST_OK, []byte("Daemon keyring successfuly bootstrapped."))
- }
- /*
- Wrapping the functionality of the Project bootstrapper for top level cleanliness
- */
- func (s SemaphoreConnection) projectBootstrapper() daemon.SockMessage {
- err := s.NewProject(YosaiProject)
- if err != nil {
- return *daemon.NewSockMessage(daemon.MsgResponse, daemon.REQUEST_FAILED, []byte(err.Error()))
- }
- err = s.AddRepository(s.Config.Repo(), s.Config.Branch())
- if err != nil {
- return *daemon.NewSockMessage(daemon.MsgResponse, daemon.REQUEST_FAILED, []byte(err.Error()))
- }
- hashiKey, err := s.Keyring.GetKey(s.KeyTagger.HashicorpVaultKeyname())
- if err != nil {
- return *daemon.NewSockMessage(daemon.MsgResponse, daemon.REQUEST_FAILED, []byte(err.Error()))
- }
- err = s.AddEnvironment(EnvironmentVariables{SecretsProviderUrl: s.Config.SecretsBackendUrl(), SecretsProviderApiKey: hashiKey.GetSecret()})
- if err != nil {
- return *daemon.NewSockMessage(daemon.MsgResponse, daemon.REQUEST_FAILED, []byte(err.Error()))
- }
- err = s.AddJobTemplate(s.Config.PlaybookName(), fmt.Sprintf("%s:%s", s.Config.Repo(), s.Config.Branch()))
- if err != nil {
- return *daemon.NewSockMessage(daemon.MsgResponse, daemon.REQUEST_FAILED, []byte(err.Error()))
- }
- return *daemon.NewSockMessage(daemon.MsgResponse, daemon.REQUEST_OK, []byte("Project successfuly bootstrapped."))
- }
- /*
- Wrapping the inventory bootstrap functionality for top level cleanliness
- */
- func (s SemaphoreConnection) inventoryBootstrapper() daemon.SockMessage {
- err := s.AddInventory(YosaiServerInventory, s.Config.VpnServer())
- if err != nil {
- return *daemon.NewSockMessage(daemon.MsgResponse, daemon.REQUEST_FAILED, []byte(err.Error()))
- }
- err = s.AddHostToInv(YosaiServerInventory, s.Config.VpnServer())
- if err != nil {
- return *daemon.NewSockMessage(daemon.MsgResponse, daemon.REQUEST_FAILED, []byte(err.Error()))
- }
- return *daemon.NewSockMessage(daemon.MsgResponse, daemon.REQUEST_OK, []byte("Inventory successfuly bootstrapped."))
- }
- func (s SemaphoreConnection) BootstrapHandler(msg daemon.SockMessage) daemon.SockMessage {
- var req SemaphoreRequest
- err := json.Unmarshal(msg.Body, &req)
- if err != nil {
- return *daemon.NewSockMessage(daemon.MsgResponse, daemon.REQUEST_FAILED, []byte(err.Error()))
- }
- switch req.Target {
- case "keys":
- return s.keyBootstrapper()
- case "project":
- return s.projectBootstrapper()
- case "inventory":
- return s.inventoryBootstrapper()
- case "all":
- bootstrapFuncs := []func() daemon.SockMessage{
- s.keyBootstrapper,
- s.inventoryBootstrapper,
- s.projectBootstrapper,
- }
- successMsg := ""
- for i := range bootstrapFuncs {
- call := bootstrapFuncs[i]
- resp := call()
- if resp.StatusCode != daemon.REQUEST_OK {
- return resp
- }
- successMsg = successMsg + resp.StatusMsg + "\n"
- }
- return *daemon.NewSockMessage(daemon.MsgResponse, daemon.REQUEST_OK, []byte(successMsg))
- default:
- return *daemon.NewSockMessage(daemon.MsgResponse, daemon.REQUEST_UNRESOLVED, []byte("Unresolved Method."))
- }
- }
- /*
- Router for handling all stuff relating to Projects
- :param msg: a daemon.SockMessage with request info
- */
- func (s SemaphoreConnection) ProjectHandler(msg daemon.SockMessage) daemon.SockMessage {
- switch msg.Method {
- case "bootstrap":
- return s.projectBootstrapper()
- case "add":
- var req SemaphoreRequest
- err := json.Unmarshal(msg.Body, &req)
- if err != nil {
- return *daemon.NewSockMessage(daemon.MsgResponse, daemon.REQUEST_FAILED, []byte(err.Error()))
- }
- err = s.NewProject(req.Target)
- if err != nil {
- return *daemon.NewSockMessage(daemon.MsgResponse, daemon.REQUEST_FAILED, []byte(err.Error()))
- }
- return *daemon.NewSockMessage(daemon.MsgResponse, daemon.REQUEST_OK, []byte("Project: "+req.Target+" successfully added."))
- case "show":
- proj, err := s.GetProjects()
- if err != nil {
- return *daemon.NewSockMessage(daemon.MsgResponse, daemon.REQUEST_FAILED, []byte(err.Error()))
- }
- b, err := json.MarshalIndent(proj, " ", " ")
- if err != nil {
- return *daemon.NewSockMessage(daemon.MsgResponse, daemon.REQUEST_FAILED, []byte(err.Error()))
- }
- return *daemon.NewSockMessage(daemon.MsgResponse, daemon.REQUEST_OK, b)
- default:
- return *daemon.NewSockMessage(daemon.MsgResponse, daemon.REQUEST_UNRESOLVED, []byte("Unresolved Method."))
- }
- }
- /*
- handler to wrap all functions relating to Tasks
- :param msg: a daemon.SockMessage that contains the request information
- */
- func (s SemaphoreConnection) TaskHandler(msg daemon.SockMessage) daemon.SockMessage {
- var req SemaphoreRequest
- err := json.Unmarshal(msg.Body, &req)
- if err != nil {
- return *daemon.NewSockMessage(daemon.MsgResponse, daemon.REQUEST_FAILED, []byte(err.Error()))
- }
- switch msg.Method {
- case "run":
- resp, err := s.StartJob(req.Target)
- if err != nil {
- return *daemon.NewSockMessage(daemon.MsgResponse, daemon.REQUEST_FAILED, []byte(err.Error()))
- }
- b, err := json.MarshalIndent(resp, " ", " ")
- if err != nil {
- return *daemon.NewSockMessage(daemon.MsgResponse, daemon.REQUEST_FAILED, []byte(err.Error()))
- }
- return *daemon.NewSockMessage(daemon.MsgResponse, daemon.REQUEST_OK, b)
- case "show":
- taskid, err := strconv.Atoi(req.Target)
- if err != nil {
- return *daemon.NewSockMessage(daemon.MsgResponse, daemon.REQUEST_FAILED, []byte(err.Error()))
- }
- taskout, err := s.GetTaskOutput(taskid)
- if err != nil {
- return *daemon.NewSockMessage(daemon.MsgResponse, daemon.REQUEST_FAILED, []byte(err.Error()))
- }
- b, err := json.MarshalIndent(taskout, " ", " ")
- if err != nil {
- return *daemon.NewSockMessage(daemon.MsgResponse, daemon.REQUEST_FAILED, []byte(err.Error()))
- }
- return *daemon.NewSockMessage(daemon.MsgResponse, daemon.REQUEST_OK, b)
- case "poll":
- taskId, err := strconv.Atoi(req.Target)
- if err != nil {
- return *daemon.NewSockMessage(daemon.MsgResponse, daemon.REQUEST_TIMEOUT, []byte(err.Error()))
- }
- err = s.PollTask(taskId, 30)
- if err != nil {
- return *daemon.NewSockMessage(daemon.MsgResponse, daemon.REQUEST_TIMEOUT, []byte(err.Error()))
- }
- return *daemon.NewSockMessage(daemon.MsgResponse, daemon.REQUEST_OK, []byte("Task: "+req.Target+" completed."))
- default:
- return *daemon.NewSockMessage(daemon.MsgResponse, daemon.REQUEST_UNRESOLVED, []byte("Unresolved Method."))
- }
- }
- /*
- Handles all of the requests relating to Hosts
- :param msg: a daemon.SockMessage containing all of the request info
- */
- func (s SemaphoreConnection) HostHandler(msg daemon.SockMessage) daemon.SockMessage {
- var req SemaphoreRequest
- err := json.Unmarshal(msg.Body, &req)
- if err != nil {
- return *daemon.NewSockMessage(daemon.MsgResponse, daemon.REQUEST_FAILED, []byte(err.Error()))
- }
- switch msg.Method {
- case "add":
- hosts := strings.Split(strings.Trim(req.Target, ","), ",")
- err := s.AddHostToInv(YosaiServerInventory, hosts...)
- if err != nil {
- return *daemon.NewSockMessage(daemon.MsgResponse, daemon.REQUEST_FAILED, []byte(err.Error()))
- }
- return *daemon.NewSockMessage(daemon.MsgRequest, daemon.REQUEST_OK, []byte(fmt.Sprintf("Host: %v added to the inventory", hosts)))
- case "delete":
- hosts := strings.Split(strings.Trim(req.Target, ","), ",")
- err := s.RemoveHostFromInv(YosaiServerInventory, hosts...)
- if err != nil {
- return *daemon.NewSockMessage(daemon.MsgResponse, daemon.REQUEST_FAILED, []byte(err.Error()))
- }
- return *daemon.NewSockMessage(daemon.MsgRequest, daemon.REQUEST_OK, []byte(fmt.Sprintf("Host: %v removed from the inventory", hosts)))
- default:
- return *daemon.NewSockMessage(daemon.MsgResponse, daemon.REQUEST_UNRESOLVED, []byte("Unresolved Method."))
- }
- }
- /*
- Implementing the router interface
- :param msg: a daemon.SockMessage containing the request data
- */
- func (s SemaphoreConnection) SemaphoreRouter(msg daemon.SockMessage) daemon.SockMessage {
- switch msg.Target {
- case "bootstrap":
- return s.BootstrapHandler(msg)
- case "project":
- return s.ProjectHandler(msg)
- case "task":
- return s.TaskHandler(msg)
- case "hosts":
- return s.HostHandler(msg)
- default:
- return *daemon.NewSockMessage(daemon.MsgResponse, daemon.REQUEST_UNRESOLVED, []byte("Unresolved Method."))
- }
- }
- /*
- ######################################################
- ############# YAML INVENTORY STRUCTS #################
- ######################################################
- */
- type YamlInventory struct {
- All yamlInvAll `yaml:"all"`
- }
- type yamlInvAll struct {
- Hosts map[string]yamlVars `yaml:"hosts"`
- }
- type yamlVars struct {
- AnsibleSshCommonArgs string `yaml:"ansible_ssh_common_args"`
- MachineType string `yaml:"machine_type"`
- MachineSubType string `yaml:"machine_subtype"`
- VpnNetworkAddress string `yaml:"vpn_network_address"`
- VpnServerPort int `yaml:"vpn_server_port"`
- ClientPubkey string `yaml:"client_public_key"`
- ClientVpnAddress string `yaml:"client_vpn_address"`
- SecretsProvider string `yaml:"secrets_provider"`
- }
- /*
- YAML inventory builder function
- :param hosts: a list of host IP addresses to add to the VPN server inventory
- */
- func (s SemaphoreConnection) YamlInventoryBuilder(hosts []string, clientPubkey string) YamlInventory {
- hostmap := map[string]yamlVars{}
- for i := range hosts {
- hostmap[hosts[i]] = yamlVars{
- AnsibleSshCommonArgs: "-o StrictHostKeyChecking=no",
- MachineType: "vpn",
- MachineSubType: "server",
- VpnNetworkAddress: s.Config.VpnServerNetwork(),
- VpnServerPort: s.Config.VpnServerPort(),
- ClientPubkey: clientPubkey,
- ClientVpnAddress: s.Config.VpnClientIpAddr(),
- SecretsProvider: s.Config.SecretsBackend()}
- }
- return YamlInventory{
- All: yamlInvAll{
- Hosts: hostmap,
- },
- }
- }
- /*
- ##########################################
- ################ ERRORS ##################
- ##########################################
- */
- type SemaphoreClientError struct {
- Msg string
- }
- // Implementing error interface
- func (s *SemaphoreClientError) Error() string {
- return fmt.Sprintf("There was an error with the call to the semaphore server: '%s'", s.Msg)
- }
- type KeyNotFound struct{ Keyname string }
- func (k *KeyNotFound) Error() string {
- return fmt.Sprintf("Key '%s' was not found in the Semaphore Keystore", k.Keyname)
- }
- type SemaphoreTimeout struct {
- Tries int
- }
- func (s *SemaphoreTimeout) Error() string {
- return "Semaphore job execution poll timed out after: " + fmt.Sprint(s.Tries) + " calls to the server."
- }
|