Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
0415b63a
Commit
0415b63a
authored
Jan 29, 2015
by
Johan Euphrosine
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
contrib/git-sync: remove sync loop, simplify logic
parent
79dd775d
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
40 additions
and
113 deletions
+40
-113
README.md
contrib/git-sync/README.md
+3
-3
main.go
contrib/git-sync/main.go
+37
-110
No files found.
contrib/git-sync/README.md
View file @
0415b63a
# git-sync
# git-sync
git-sync is a command that p
eriodically sync
a git repository to a local directory.
git-sync is a command that p
ull
a git repository to a local directory.
It can be used to source a container volume with the content of a git repo.
It can be used to source a container volume with the content of a git repo.
...
@@ -10,7 +10,7 @@ It can be used to source a container volume with the content of a git repo.
...
@@ -10,7 +10,7 @@ It can be used to source a container volume with the content of a git repo.
# build the container
# build the container
docker build -t git-sync .
docker build -t git-sync .
# run the git-sync container
# run the git-sync container
docker run -d
-e GIT_SYNC_INTERVAL=1s -e GIT_SYNC_REPO=https://github.com/GoogleCloudPlatform/kubernetes -e GIT_SYNC_BRANCH=gh-pages
-v /git-data:/git git-sync
docker run -d
GIT_SYNC_REPO=https://github.com/GoogleCloudPlatform/kubernetes -e GIT_SYNC_BRANCH=gh-pages -r HEAD
-v /git-data:/git git-sync
# run a nginx container to serve sync'ed content
# run a nginx container to serve sync'ed content
docker run -d -p 8080:80 -v /git-data
/HEAD
:/usr/share/nginx/html nginx
docker run -d -p 8080:80 -v /git-data:/usr/share/nginx/html nginx
```
```
contrib/git-sync/main.go
View file @
0415b63a
...
@@ -14,27 +14,24 @@ See the License for the specific language governing permissions and
...
@@ -14,27 +14,24 @@ See the License for the specific language governing permissions and
limitations under the License.
limitations under the License.
*/
*/
// git-sync is a command that p
eriodically sync
a git repository to a local directory.
// git-sync is a command that p
ull
a git repository to a local directory.
package
main
// import "github.com/GoogleCloudPlatform/kubernetes/git-sync"
package
main
// import "github.com/GoogleCloudPlatform/kubernetes/
contrib/
git-sync"
import
(
import
(
"flag"
"flag"
"fmt"
"fmt"
"log"
"log"
"net/http"
"os"
"os"
"os/exec"
"os/exec"
"path"
"path"
"strings"
"strings"
"time"
)
)
var
interval
=
flag
.
String
(
"interval"
,
env
(
"GIT_SYNC_INTERVAL"
,
"60s"
),
"git pull interval"
)
var
flRepo
=
flag
.
String
(
"repo"
,
env
(
"GIT_SYNC_REPO"
,
""
),
"git repo url"
)
var
repo
=
flag
.
String
(
"repo"
,
env
(
"GIT_SYNC_REPO"
,
""
),
"git repo url"
)
var
flBranch
=
flag
.
String
(
"branch"
,
env
(
"GIT_SYNC_BRANCH"
,
"master"
),
"git branch"
)
var
branch
=
flag
.
String
(
"branch"
,
env
(
"GIT_SYNC_BRANCH"
,
"master"
),
"git branch"
)
var
flRev
=
flag
.
String
(
"rev"
,
env
(
"GIT_SYNC_BRANCH"
,
"HEAD"
),
"git rev"
)
var
handler
=
flag
.
String
(
"handler"
,
env
(
"GIT_SYNC_HANDLER"
,
"/"
),
"web hook handler"
)
var
flDest
=
flag
.
String
(
"dest"
,
env
(
"GIT_SYNC_DEST"
,
""
),
"destination path"
)
var
dest
=
flag
.
String
(
"dest"
,
env
(
"GIT_SYNC_DEST"
,
""
),
"destination path"
)
func
env
(
key
,
def
string
)
string
{
func
env
(
key
,
def
string
)
string
{
if
env
:=
os
.
Getenv
(
key
);
env
!=
""
{
if
env
:=
os
.
Getenv
(
key
);
env
!=
""
{
...
@@ -43,125 +40,55 @@ func env(key, def string) string {
...
@@ -43,125 +40,55 @@ func env(key, def string) string {
return
def
return
def
}
}
const
usage
=
"usage: GIT_SYNC_REPO= GIT_SYNC_DEST= [GIT_SYNC_
INTERVAL= GIT_SYNC_BRANCH= GIT_SYNC_HANDLER=] git-sync -repo GIT_REPO_URL -dest PATH [-interval -branch -handler
]"
const
usage
=
"usage: GIT_SYNC_REPO= GIT_SYNC_DEST= [GIT_SYNC_
BRANCH=] git-sync -repo GIT_REPO_URL -dest PATH [-branch
]"
func
main
()
{
func
main
()
{
flag
.
Parse
()
flag
.
Parse
()
if
*
repo
==
""
||
*
d
est
==
""
{
if
*
flRepo
==
""
||
*
flD
est
==
""
{
flag
.
Usage
()
flag
.
Usage
()
log
.
Fatal
(
usage
)
log
.
Fatal
(
usage
)
}
}
pullInterval
,
err
:=
time
.
ParseDuration
(
*
interval
)
if
err
!=
nil
{
log
.
Fatalf
(
"error parsing time duration %q: %v"
,
*
interval
,
err
)
}
if
_
,
err
:=
exec
.
LookPath
(
"git"
);
err
!=
nil
{
if
_
,
err
:=
exec
.
LookPath
(
"git"
);
err
!=
nil
{
log
.
Fatalf
(
"required git executable not found: %v"
,
err
)
log
.
Fatalf
(
"required git executable not found: %v"
,
err
)
}
}
repo
,
err
:=
NewRepo
()
if
err
:=
syncRepo
(
*
flRepo
,
*
flDest
,
*
flBranch
,
*
flRev
);
err
!=
nil
{
if
err
!=
nil
{
log
.
Fatalf
(
"error syncing repo: %v"
,
err
)
log
.
Fatalf
(
"error creating repo: %v"
,
err
)
}
}
syncc
:=
make
(
chan
struct
{})
tick
:=
time
.
Tick
(
pullInterval
)
go
func
()
{
for
{
repo
.
Sync
()
select
{
case
<-
tick
:
case
<-
syncc
:
}
}
}()
http
.
HandleFunc
(
*
handler
,
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
syncc
<-
struct
{}{}
})
log
.
Fatal
(
http
.
ListenAndServe
(
":8080"
,
nil
))
}
type
Repo
struct
{
basePath
string
mirrorPath
string
lastRev
string
}
}
// NewRepo initalize a local bare repository mirror.
// syncRepo syncs the branch of a given repository to the destination at the given rev.
func
NewRepo
()
(
*
Repo
,
error
)
{
func
syncRepo
(
repo
,
dest
,
branch
,
rev
string
)
error
{
mirrorRepoPath
:=
path
.
Join
(
*
dest
,
".git"
)
gitRepoPath
:=
path
.
Join
(
dest
,
".git"
)
_
,
err
:=
os
.
Stat
(
mirrorRepoPath
)
_
,
err
:=
os
.
Stat
(
gitRepoPath
)
if
err
==
nil
{
switch
{
log
.
Printf
(
"found existing mirror repo %q"
,
mirrorRepoPath
)
case
os
.
IsNotExist
(
err
)
:
return
&
Repo
{
// clone repo
basePath
:
*
dest
,
cmd
:=
exec
.
Command
(
"git"
,
"clone"
,
"--no-checkout"
,
"-b"
,
branch
,
repo
,
dest
)
mirrorPath
:
mirrorRepoPath
,
output
,
err
:=
cmd
.
CombinedOutput
()
},
nil
if
err
!=
nil
{
}
return
fmt
.
Errorf
(
"error cloning repo %q: %v: %s"
,
strings
.
Join
(
cmd
.
Args
,
" "
),
err
,
string
(
output
))
if
!
os
.
IsNotExist
(
err
)
{
}
return
nil
,
fmt
.
Errorf
(
"error checking repo %q: %v"
,
mirrorRepoPath
,
err
)
log
.
Printf
(
"clone %q: %s"
,
repo
,
string
(
output
))
}
case
err
!=
nil
:
cmd
:=
exec
.
Command
(
"git"
,
"clone"
,
"--mirror"
,
"-b"
,
*
branch
,
*
repo
,
mirrorRepoPath
)
return
fmt
.
Errorf
(
"error checking if repo exist %q: %v"
,
gitRepoPath
,
err
)
output
,
err
:=
cmd
.
CombinedOutput
()
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"error cloning repo %q: %v:"
,
strings
.
Join
(
cmd
.
Args
,
" "
),
err
)
}
}
log
.
Printf
(
"clone %q: %s"
,
*
repo
,
string
(
output
))
return
&
Repo
{
basePath
:
*
dest
,
mirrorPath
:
mirrorRepoPath
,
},
nil
}
// Sync fetch new revision from the origin remote in the bare repository
// fetch branch
// create a new checkout named after the revision
cmd
:=
exec
.
Command
(
"git"
,
"fetch"
,
"origin"
,
branch
)
// update HEAD symlink to point to the new revision
cmd
.
Dir
=
dest
func
(
r
*
Repo
)
Sync
()
{
cmd
:=
exec
.
Command
(
"git"
,
"fetch"
,
"origin"
,
*
branch
)
cmd
.
Dir
=
r
.
mirrorPath
output
,
err
:=
cmd
.
CombinedOutput
()
output
,
err
:=
cmd
.
CombinedOutput
()
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Printf
(
"error running command %q: %v"
,
strings
.
Join
(
cmd
.
Args
,
" "
),
err
)
return
fmt
.
Errorf
(
"error running command %q: %v: %s"
,
strings
.
Join
(
cmd
.
Args
,
" "
),
err
,
string
(
output
))
return
}
log
.
Printf
(
"fetch: %s"
,
string
(
output
))
cmd
=
exec
.
Command
(
"git"
,
"rev-parse"
,
"HEAD"
)
cmd
.
Dir
=
r
.
mirrorPath
output
,
err
=
cmd
.
CombinedOutput
()
if
err
!=
nil
{
log
.
Printf
(
"error running command %q: %v"
,
strings
.
Join
(
cmd
.
Args
,
" "
),
err
)
return
}
rev
:=
strings
.
TrimSpace
(
string
(
output
))
if
rev
==
r
.
lastRev
{
log
.
Printf
(
"no new rev since last check %q"
,
rev
)
return
}
}
r
.
lastRev
=
rev
log
.
Printf
(
"fetch %q: %s"
,
branch
,
string
(
output
))
log
.
Printf
(
"HEAD is: %q"
,
rev
)
repoPath
:=
path
.
Join
(
r
.
basePath
,
rev
)
// reset working copy
_
,
err
=
os
.
Stat
(
repoPath
)
cmd
=
exec
.
Command
(
"git"
,
"reset"
,
"--hard"
,
rev
)
if
err
==
nil
{
cmd
.
Dir
=
dest
log
.
Printf
(
"found existing repo: %q"
,
repoPath
)
return
}
if
!
os
.
IsNotExist
(
err
)
{
log
.
Printf
(
"error stating repo %q: %v"
,
repoPath
,
err
)
return
}
cmd
=
exec
.
Command
(
"git"
,
"clone"
,
r
.
mirrorPath
,
repoPath
)
output
,
err
=
cmd
.
CombinedOutput
()
output
,
err
=
cmd
.
CombinedOutput
()
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Printf
(
"error running command %q : %v"
,
strings
.
Join
(
cmd
.
Args
,
" "
),
err
)
return
fmt
.
Errorf
(
"error running command %q : %v: %s"
,
strings
.
Join
(
cmd
.
Args
,
" "
),
err
,
string
(
output
))
return
}
log
.
Printf
(
"clone %q: %v"
,
repoPath
,
string
(
output
))
tempPath
:=
path
.
Join
(
r
.
basePath
,
"HEAD."
+
rev
)
if
err
:=
os
.
Symlink
(
rev
,
tempPath
);
err
!=
nil
{
log
.
Printf
(
"error creating temporary symlink %q: %v"
,
tempPath
,
err
)
return
}
linkPath
:=
path
.
Join
(
r
.
basePath
,
"HEAD"
)
if
err
:=
os
.
Rename
(
tempPath
,
linkPath
);
err
!=
nil
{
log
.
Printf
(
"error moving symlink %q: %v"
,
linkPath
,
err
)
return
}
}
log
.
Printf
(
"reset %q: %v"
,
rev
,
string
(
output
))
return
nil
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment