Skip to content

Commit 7603bfc

Browse files
committed
Initial commit
0 parents  commit 7603bfc

File tree

29 files changed

+3269
-0
lines changed

29 files changed

+3269
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
harbor/bindata.go
2+
harbor/harbor
3+
.vscode
4+

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+220
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
# harbor
2+
3+
harbor is API server manages the docker containers
4+
5+
```
6+
% harbor -p :9999 -r "10000:12000" -w ./harbor
7+
```
8+
9+
options
10+
11+
```
12+
-p string
13+
server listen port (default ":8080")
14+
-r string
15+
port range for containers (default "10000:12000")
16+
-w string
17+
application workspace path (default "./harbor")
18+
```
19+
20+
# API
21+
22+
## Register Project
23+
24+
```
25+
% curl -XPOST -d 'payload={"name":"${PROJECT_NAME}", "repo":"${REPO_NAME}' localhost:9999/register
26+
```
27+
28+
response
29+
30+
```
31+
{
32+
"name": "project name",
33+
"repo": "repositry name",
34+
"branch": [
35+
"deployed branch informations ([]branch.Config)"
36+
],
37+
"created_at": "create time"
38+
}
39+
```
40+
41+
## Unregister project
42+
43+
```
44+
% curl -XPOST -d 'payload={"name":"${PROJECT_NAME}"}' localhost:9999/unregister
45+
```
46+
47+
response
48+
49+
```
50+
{
51+
"portsAvailable": "available port range",
52+
"portsAllocated": [
53+
"allocated port list ([]int)"
54+
],
55+
}
56+
```
57+
58+
## Get project list
59+
60+
```
61+
curl -XGET localhost:9999/list
62+
```
63+
64+
response
65+
66+
```
67+
{
68+
"projects: [
69+
"project list ([]project.Config)
70+
]
71+
}
72+
```
73+
74+
## Get deploy branch status
75+
76+
```
77+
% curl -XGET localhost:9999/br?name=${PROJECT_NAME}&branch=${BRANCH_NAME}
78+
```
79+
80+
response
81+
82+
```
83+
{
84+
"name": "branch name",
85+
"port": [
86+
"allocated port list ([]int)
87+
],
88+
"state": "deploy state (0:unknown 1:started 2:done)",
89+
"work": "deploy path",
90+
"notice": "deploy message",
91+
"deployed_at": "deploy time"
92+
}
93+
```
94+
95+
## docker-compose up
96+
97+
```
98+
% curl -XPOST -d 'payload={"name":"${PROJECT_NAME}", "branch":"${BRANCH_NAME}' localhost:9999/up
99+
```
100+
101+
response
102+
103+
```
104+
# same as /br
105+
```
106+
107+
## docker-compose down
108+
109+
```
110+
% curl -XPOST -d 'payload={"name":"${PROJECT_NAME}", "branch":"${BRANCH_NAME}' localhost:9999/down
111+
```
112+
113+
response
114+
115+
```
116+
# same as /register
117+
```
118+
119+
# Examples
120+
121+
execute server
122+
123+
```
124+
% harbor -p :9999 -r "10000:12000" -w /tmp/harbor
125+
```
126+
127+
registe project to harbor
128+
129+
```
130+
% curl -XPOST -d 'payload={"name":"${PROJECT_NAME}", "repo":"${REPO_NAME}' localhost:9999/register
131+
```
132+
133+
create project like below
134+
135+
```
136+
% tree hatajoe/umedago (hoge) hatajoe
137+
.
138+
├── docker-compose.yml
139+
└── public
140+
└── index.html
141+
142+
1 directory, 3 files
143+
```
144+
145+
docker-compose.yml
146+
147+
```
148+
nginx:
149+
image: nginx
150+
ports:
151+
- "${PORT1}:80"
152+
volumes:
153+
- ./public/:/usr/share/nginx/html/
154+
```
155+
156+
index.html
157+
158+
```
159+
Hello, World!
160+
```
161+
162+
create repository and commit
163+
164+
```
165+
% git init
166+
% git add .
167+
% git commit -m "Initial commit"
168+
% hub create
169+
```
170+
171+
add .git/hooks/pre-push and add executable permission
172+
173+
```
174+
#!/bin/sh
175+
176+
PROJECT_NAME="replace here"
177+
178+
HARBOR_DOMAIN=localhost:9999
179+
BRANCH=$(git rev-parse --abbrev-ref HEAD)
180+
181+
RES=`curl -XGET http://$HARBOR_DOMAIN/br?name=$PROJECT_NAME\&branch=$BRANCH`
182+
WORK=`echo ${RES} | jq -r '.work'`
183+
184+
# deploy
185+
186+
mkdir -p ${WORK}
187+
rsync -rv --exclude=.git . ${WORK}
188+
189+
# docker-compose up
190+
FLG=1
191+
curl -XPOST -d "payload={\"name\":\"$PROJECT_NAME\", \"branch\":\"$BRANCH\"}" http://$HARBOR_DOMAIN/up
192+
193+
while [ $FLG == 1 ]
194+
do
195+
sleep 1
196+
exit;
197+
RES=`curl -XGET http://$HARBOR_DOMAIN/br?name=$PROJECT_NAME\&branch=$BRANCH`
198+
if [ $? == 0 ]; then
199+
STATE=`echo $RES | jq -r '.state'`
200+
if [ "$STATE" == "2" ]; then
201+
FLG=2
202+
fi
203+
fi
204+
done
205+
206+
exit 0
207+
```
208+
209+
deploy
210+
211+
```
212+
% git push -u origin master
213+
```
214+
215+
maybe this works...
216+
217+
```
218+
% open http://localhost:10000
219+
```
220+

controllers/br/br.go

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package br
2+
3+
import (
4+
"net/http"
5+
6+
myctx "github.com/dev-cloverlab/harbor/middleware/context"
7+
8+
"github.com/dev-cloverlab/harbor/models/branch"
9+
"github.com/dev-cloverlab/harbor/models/project"
10+
"github.com/syndtr/goleveldb/leveldb"
11+
)
12+
13+
func Handler(db *leveldb.DB, w http.ResponseWriter, r *http.Request) {
14+
15+
name := r.URL.Query().Get("name")
16+
branchName := r.URL.Query().Get("branch")
17+
18+
if name == "" {
19+
http.Error(w, "name is required", http.StatusBadRequest)
20+
return
21+
}
22+
23+
if branchName == "" {
24+
http.Error(w, "branch is required", http.StatusBadRequest)
25+
return
26+
}
27+
28+
// load global settings from request context
29+
globalConf := &myctx.Config{}
30+
globalConf.FromJson(r.Context().Value(myctx.ConfigKey).([]byte))
31+
32+
projConf := project.Config{}
33+
if err := projConf.Load(name, db); err != nil {
34+
http.Error(w, err.Error(), http.StatusBadRequest)
35+
return
36+
}
37+
branchConf := projConf.GetBranchByName(branchName)
38+
if branchConf == nil {
39+
branchConf = branch.NewConfig(branchName, projConf.Name, projConf.RepoName, globalConf.WorkSpace)
40+
}
41+
j, err := branchConf.ToJson()
42+
if err != nil {
43+
http.Error(w, err.Error(), http.StatusInternalServerError)
44+
return
45+
}
46+
w.Write(j)
47+
}

0 commit comments

Comments
 (0)