-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodeci.go
172 lines (151 loc) · 5.52 KB
/
codeci.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package main
import (
"fmt"
"io/ioutil"
"gopkg.in/yaml.v2"
"strings"
"os/exec"
"os"
)
const version = "0.2.1"
func check(e error){
if e != nil {
panic(e)
}
}
type CodeCi struct {
Os string `yaml:"os"`
Language string `yaml:"language"`
Image string `yaml:"image"`
Script []string `yaml:"script"`
}
func dockerfileName() string {
return "Dockerfile.codeci"
}
func dockercomposeName() string {
return "docker-compose.codeci.yml"
}
func onlytestName() string {
return "onlytest.codeci.sh"
}
func testName() string {
return "test.codeci.sh"
}
func createTestScript(codeci CodeCi) string {
jobInfo := []string{"echo 'Job Node Info: '", "echo \n", "echo 'uname -a'", "uname -a", "echo \n", "echo 'df -h'", "df -h", "echo \n", "echo 'free -m'", "free -m", "echo \n", "echo 'bash --version'", "bash --version", "echo \n", "echo 'lscpu'", "lscpu", "echo \n", "echo 'lsb_release -a'", "lsb_release -a", "echo \n", "echo 'service --status-all'", "service --status-all", "echo \n", "echo 'dpkg -l'", "dpkg -l", "echo \n", "echo \n"}
s := []string{"#!/bin/bash", "\n", "\n", strings.Join(jobInfo, "\n") , "\n", "echo 'running your commands: '", "\n", "sleep 1", "\n","set -ex", "\n", strings.Join(codeci.Script, "\n"), "\n"}
return strings.Join(s, "")
}
func createDockerFile(codeci CodeCi) string{
if codeci.Image != "" {
s := []string{"FROM ", codeci.Image, "\n", "ADD . /app\nWORKDIR /app\nCMD [\"bash\", \"", testName(),"\"]", "\n"}
return strings.Join(s, "")
}
if codeci.Language == "none" {
s := []string{"FROM therickys93/", codeci.Os, "\n", "ADD . /app\nWORKDIR /app\nCMD [\"bash\", \"", testName(),"\"]", "\n"}
return strings.Join(s, "")
} else {
s := []string{"FROM therickys93/", codeci.Os, codeci.Language, "\n", "ADD . /app\nWORKDIR /app\nCMD [\"bash\", \"", testName(),"\"]", "\n"}
return strings.Join(s, "")
}
}
func codeCIWhalesay() string {
return "image: docker/whalesay\nscript:\n - cowsay Hello CodeCI!"
}
func main() {
filename := "codeci.yml"
data := []byte{}
if len(os.Args) > 1 {
if os.Args[1] == "--version" {
fmt.Printf("%s version: %s\n", os.Args[0], version)
os.Exit(0)
} else if os.Args[1] == "--help" {
fmt.Printf("usage: %s --> runs the build and search for the codeci.yml\n", os.Args[0])
fmt.Printf("usage: %s --version --> show the current version\n", os.Args[0])
fmt.Printf("usage: %s -f codeci.whateveryouwant.yml --> specify the name of your codeci file\n", os.Args[0])
fmt.Printf("usage: %s test --> test the installation\n", os.Args[0])
os.Exit(0)
} else if os.Args[1] == "-f" {
if strings.HasPrefix(os.Args[2], "codeci") && strings.HasSuffix(os.Args[2], ".yml") {
filename = os.Args[2]
} else {
os.Exit(1)
}
} else if os.Args[1] == "test" {
data = []byte(codeCIWhalesay())
} else {
os.Exit(1)
}
}
filenames := []string{"./", filename}
var err error
if strings.EqualFold(string(data), "") {
data, err = ioutil.ReadFile(strings.Join(filenames, ""))
check(err)
}
fmt.Printf("reading the provided codeci.yml file...\n\n")
fmt.Print(string(data))
fmt.Printf("\n\n")
var codeci CodeCi
err = yaml.Unmarshal([]byte(string(data)), &codeci)
check(err)
fmt.Printf("Creating temp files...\n")
// create the test.sh file
d1 := []byte(createTestScript(codeci))
err = ioutil.WriteFile(testName(), d1, 0644)
check(err)
// create the Dockerfile
d1 = []byte(createDockerFile(codeci))
err = ioutil.WriteFile(dockerfileName(), d1, 0644)
check(err)
// create the docker-compose.yml file
s := []string{"sut:\n", " build: .\n", " dockerfile: ", dockerfileName(), "\n"}
d1 = []byte(strings.Join(s, ""))
err = ioutil.WriteFile(dockercomposeName(), d1, 0644)
check(err)
// create the onlytest.sh file
s = []string{"#!/bin/bash", "\n", "\n",
"docker-compose -f ", dockercomposeName() ," -p ci build", "\n",
"echo running the script...", "\n",
"echo -e '\n'", "\n",
"docker-compose -f ", dockercomposeName(), " -p ci up -d", "\n",
"docker logs -f ci_sut_1", "\n",
"echo -e '\n'", "\n",
"echo 'BUILD EXIT CODE:'", "\n",
"docker wait ci_sut_1", "\n",
"EXIT_STATUS=$(docker wait ci_sut_1)", "\n",
"if [ $EXIT_STATUS == 0 ]; then echo -e '\nBUILD SUCCESS\n'; else echo -e '\nBUILD FAILED\n'; fi", "\n",
"docker-compose -f ", dockercomposeName(), " -p ci kill", "\n",
"docker rm ci_sut_1", "\n",
"docker rmi ci_sut", "\n",
"exit $EXIT_STATUS"}
d1 = []byte(strings.Join(s, ""))
err = ioutil.WriteFile(onlytestName(), d1, 0644)
check(err)
// run the script onlytest.sh
fmt.Print("run the build...\n")
cmd := exec.Command("/bin/bash", onlytestName())
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Start()
exitstatus := 0
if err != nil {
exitstatus = 1
}
err = cmd.Wait()
if err != nil {
exitstatus = 2
}
// remove all the files
fmt.Print("removing the temp files...\n")
// remove test file
os.Remove(testName())
// remove Dockerfile
os.Remove(dockerfileName())
// remove only test file
os.Remove(onlytestName())
// remove docker compose file
os.Remove(dockercomposeName())
fmt.Print("done!\n")
os.Exit(exitstatus)
}