-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdeployment_job.go
120 lines (94 loc) · 2.77 KB
/
deployment_job.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
We create DeploymentJob for each deployment on each server. If Environment has 5 MachineIds in Environment where we should deploy we should create 5 DeploymentJobs
*/
package main
import (
"fmt"
"github.com/rqlite/gorqlite"
)
const StatusToDeploy = "to_deploy"
const StatusInProgress = "in_progress"
const StatusDeployed = "deployed"
type DeploymentJob struct {
Id string
Status string
DeploymentId string
MachineId string
}
func addDeploymentJob(job DeploymentJob) DeploymentJob {
id, err := NanoId(7)
if err != nil {
fmt.Println("Cannot generate new NanoId for DeploymentJob:", err)
return job
}
job.Id = id
_, err = connection.WriteParameterized(
[]gorqlite.ParameterizedStatement{
{
Query: "INSERT INTO DeploymentJob( Id, Status, DeploymentId, MachineId) VALUES(?, ?, ?, ?)",
Arguments: []interface{}{job.Id, job.Status, job.DeploymentId, job.MachineId},
},
},
)
if err != nil {
fmt.Printf(" Cannot write to DeploymentJob table: %s\n", err.Error())
}
return job
}
func getDeploymentJobsByStatus(status string) []DeploymentJob {
rows, err := connection.QueryOneParameterized(
gorqlite.ParameterizedStatement{
Query: "SELECT Id, Status, DeploymentId, MachineId from DeploymentJob WHERE STATUS = ?",
Arguments: []interface{}{status},
},
)
return handleDeploymentJobQuery(rows, err)
}
func getDeploymentJobsByDeploymentIdAndStatus(deploymentId string, status string) []DeploymentJob {
rows, err := connection.QueryOneParameterized(
gorqlite.ParameterizedStatement{
Query: "SELECT Id, Status, DeploymentId, MachineId from DeploymentJob WHERE DeploymentId = ? AND Status = ?",
Arguments: []interface{}{deploymentId, status},
},
)
return handleDeploymentJobQuery(rows, err)
}
func handleDeploymentJobQuery(rows gorqlite.QueryResult, err error) []DeploymentJob {
var jobs = []DeploymentJob{}
if err != nil {
fmt.Printf(" Cannot read from DeploymentJob table: %s\n", err.Error())
}
for rows.Next() {
var Id string
var Status string
var DeploymentId string
var MachineId string
err := rows.Scan(&Id, &Status, &DeploymentId, &MachineId)
if err != nil {
fmt.Printf(" Cannot run Scan: %s\n", err.Error())
}
loadedJob := DeploymentJob{
Id: Id,
Status: Status,
DeploymentId: DeploymentId,
MachineId: MachineId,
}
jobs = append(jobs, loadedJob)
}
return jobs
}
func updateDeploymentJobStatus(job DeploymentJob, status string) error {
_, err := connection.WriteParameterized(
[]gorqlite.ParameterizedStatement{
{
Query: "UPDATE DeploymentJob SET Status = ? WHERE Id = ?",
Arguments: []interface{}{status, job.Id},
},
},
)
if err != nil {
fmt.Printf("Cannot update a row in DeploymentJob: %s\n", err.Error())
return err
}
return nil
}