Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
hatajoe committed Apr 11, 2017
0 parents commit 7603bfc
Show file tree
Hide file tree
Showing 29 changed files with 3,269 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
harbor/bindata.go
harbor/harbor
.vscode

21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
220 changes: 220 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
# harbor

harbor is API server manages the docker containers

```
% harbor -p :9999 -r "10000:12000" -w ./harbor
```

options

```
-p string
server listen port (default ":8080")
-r string
port range for containers (default "10000:12000")
-w string
application workspace path (default "./harbor")
```

# API

## Register Project

```
% curl -XPOST -d 'payload={"name":"${PROJECT_NAME}", "repo":"${REPO_NAME}' localhost:9999/register
```

response

```
{
"name": "project name",
"repo": "repositry name",
"branch": [
"deployed branch informations ([]branch.Config)"
],
"created_at": "create time"
}
```

## Unregister project

```
% curl -XPOST -d 'payload={"name":"${PROJECT_NAME}"}' localhost:9999/unregister
```

response

```
{
"portsAvailable": "available port range",
"portsAllocated": [
"allocated port list ([]int)"
],
}
```

## Get project list

```
curl -XGET localhost:9999/list
```

response

```
{
"projects: [
"project list ([]project.Config)
]
}
```

## Get deploy branch status

```
% curl -XGET localhost:9999/br?name=${PROJECT_NAME}&branch=${BRANCH_NAME}
```

response

```
{
"name": "branch name",
"port": [
"allocated port list ([]int)
],
"state": "deploy state (0:unknown 1:started 2:done)",
"work": "deploy path",
"notice": "deploy message",
"deployed_at": "deploy time"
}
```

## docker-compose up

```
% curl -XPOST -d 'payload={"name":"${PROJECT_NAME}", "branch":"${BRANCH_NAME}' localhost:9999/up
```

response

```
# same as /br
```

## docker-compose down

```
% curl -XPOST -d 'payload={"name":"${PROJECT_NAME}", "branch":"${BRANCH_NAME}' localhost:9999/down
```

response

```
# same as /register
```

# Examples

execute server

```
% harbor -p :9999 -r "10000:12000" -w /tmp/harbor
```

registe project to harbor

```
% curl -XPOST -d 'payload={"name":"${PROJECT_NAME}", "repo":"${REPO_NAME}' localhost:9999/register
```

create project like below

```
% tree hatajoe/umedago (hoge) hatajoe
.
├── docker-compose.yml
└── public
└── index.html
1 directory, 3 files
```

docker-compose.yml

```
nginx:
image: nginx
ports:
- "${PORT1}:80"
volumes:
- ./public/:/usr/share/nginx/html/
```

index.html

```
Hello, World!
```

create repository and commit

```
% git init
% git add .
% git commit -m "Initial commit"
% hub create
```

add .git/hooks/pre-push and add executable permission

```
#!/bin/sh
PROJECT_NAME="replace here"
HARBOR_DOMAIN=localhost:9999
BRANCH=$(git rev-parse --abbrev-ref HEAD)
RES=`curl -XGET http://$HARBOR_DOMAIN/br?name=$PROJECT_NAME\&branch=$BRANCH`
WORK=`echo ${RES} | jq -r '.work'`
# deploy
mkdir -p ${WORK}
rsync -rv --exclude=.git . ${WORK}
# docker-compose up
FLG=1
curl -XPOST -d "payload={\"name\":\"$PROJECT_NAME\", \"branch\":\"$BRANCH\"}" http://$HARBOR_DOMAIN/up
while [ $FLG == 1 ]
do
sleep 1
exit;
RES=`curl -XGET http://$HARBOR_DOMAIN/br?name=$PROJECT_NAME\&branch=$BRANCH`
if [ $? == 0 ]; then
STATE=`echo $RES | jq -r '.state'`
if [ "$STATE" == "2" ]; then
FLG=2
fi
fi
done
exit 0
```

deploy

```
% git push -u origin master
```

maybe this works...

```
% open http://localhost:10000
```

47 changes: 47 additions & 0 deletions controllers/br/br.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package br

import (
"net/http"

myctx "github.com/dev-cloverlab/harbor/middleware/context"

"github.com/dev-cloverlab/harbor/models/branch"
"github.com/dev-cloverlab/harbor/models/project"
"github.com/syndtr/goleveldb/leveldb"
)

func Handler(db *leveldb.DB, w http.ResponseWriter, r *http.Request) {

name := r.URL.Query().Get("name")
branchName := r.URL.Query().Get("branch")

if name == "" {
http.Error(w, "name is required", http.StatusBadRequest)
return
}

if branchName == "" {
http.Error(w, "branch is required", http.StatusBadRequest)
return
}

// load global settings from request context
globalConf := &myctx.Config{}
globalConf.FromJson(r.Context().Value(myctx.ConfigKey).([]byte))

projConf := project.Config{}
if err := projConf.Load(name, db); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
branchConf := projConf.GetBranchByName(branchName)
if branchConf == nil {
branchConf = branch.NewConfig(branchName, projConf.Name, projConf.RepoName, globalConf.WorkSpace)
}
j, err := branchConf.ToJson()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(j)
}
Loading

0 comments on commit 7603bfc

Please sign in to comment.