Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ func Start(c *cli.Context) error {
codeDir := ""
if s.Code != "" {
code := code.New(s)
if code.Key != "" {
log.Debug("Step: code.PrivateRepo")
err = code.PrivateRepo()
if err != nil {
return err
}
}

codeDir, err = code.Get()
if err != nil {
return err
Expand Down
6 changes: 6 additions & 0 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ func Update(c *cli.Context) error {
codeUpdated := false
if s.Code != "" {
code := code.New(s)
if code.Key != "" {
err = code.PrivateRepo()
if err != nil {
return err
}
}

codeUpdated, err = code.CheckUpdate()
if err != nil {
Expand Down
141 changes: 141 additions & 0 deletions code/code.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package code

import (
"fmt"
"io"
"io/ioutil"
"net/url"
"os"
"os/exec"
"path"
"regexp"
"sort"
"time"

log "github.com/Sirupsen/logrus"
"github.com/mobingilabs/go-modaemon/server_config"
"github.com/mobingilabs/go-modaemon/util"
)
Expand All @@ -15,6 +21,7 @@ type Code struct {
URL string
Ref string
Path string
Key string
}

type Dirs []os.FileInfo
Expand All @@ -38,6 +45,7 @@ func New(s *serverConfig.Config) *Code {
return &Code{
URL: s.Code,
Ref: ref,
Key: s.GitPrivateKey,
}
}

Expand Down Expand Up @@ -95,3 +103,136 @@ func (c *Code) Get() (string, error) {
err := g.get()
return g.path, err
}

func (c *Code) PrivateRepo() error {
err := createIdentityFile(c.Key)
if err != nil {
return err
}

url, err := parseURL(c.URL)
if err != nil {
return err
}

if url.Scheme == "git" && url.Host == "github.com" {
c.URL = convertGithubGitURLToSSH(url)
log.Debugf("Converted URL is %s.", c.URL)
}

err = checkKnownHosts(url)
if err != nil {
return err
}

err = writeSshConfig(url)
if err != nil {
return err
}

return nil
}

func createIdentityFile(key string) error {
log.Debug("Step: createIdentityFile")
sshDir := "/root/.ssh"
sshKey := path.Join(sshDir, "id_code")

if !util.FileExists(sshDir) {
if err := os.Mkdir(sshDir, 0700); err != nil {
return err
}
}
if util.FileExists(sshKey) {
if err := os.Remove(sshKey); err != nil {
return err
}
}

log.Debugf("Create IdentityFile %s", sshKey)
err := ioutil.WriteFile(sshKey, []byte(key), 0600)
if err != nil {
return err
}
return nil
}

// ref. `man git-clone`
// The following syntaxes may be used.
// - ssh://[user@]host.xz[:port]/path/to/repo.git/
// - git://host.xz[:port]/path/to/repo.git/
// - http[s]://host.xz[:port]/path/to/repo.git/
// - ftp[s]://host.xz[:port]/path/to/repo.git/
// - [user@]host.xz:path/to/repo.git/
var hasSchemeSyntax = regexp.MustCompile("^[^:]+://")
var scpLikeSyntax = regexp.MustCompile("^([^@]+@)?([^:]+):/?(.+)$")

func parseURL(rawURL string) (*url.URL, error) {
log.Debug("Step: parseURL")
if !hasSchemeSyntax.MatchString(rawURL) && scpLikeSyntax.MatchString(rawURL) {
matched := scpLikeSyntax.FindStringSubmatch(rawURL)
user := matched[1]
host := matched[2]
path := matched[3]
rawURL = fmt.Sprintf("ssh://%s%s/%s", user, host, path)
}

u, err := url.Parse(rawURL)
if err != nil {
return u, err
}

return u, nil
}

func convertGithubGitURLToSSH(url *url.URL) string {
return fmt.Sprintf("ssh://[email protected]%s", url.Path)
}

func checkKnownHosts(url *url.URL) error {
log.Debug("Step: checkKnownHosts")
out, err := exec.Command("ssh-keygen", "-F", url.Host).Output()
if string(out) == "" && err != nil {
out, err = exec.Command("ssh-keyscan", url.Host).Output()
if err != nil {
return err
}
if string(out) == "" {
return fmt.Errorf("%s's ssh public key is empty", url.Host)
}

kh := "/root/.ssh/known_hosts"
log.Debugf("Add %s's public key to %s", url.Host, kh)

file, err := os.OpenFile(kh, os.O_RDWR|os.O_APPEND, 0644)
if err != nil {
return err
}
defer file.Close()
io.WriteString(file, string(out))
return nil
}
return nil
}

func writeSshConfig(url *url.URL) error {
log.Debug("Step: writeSshConfig")
c := `Host %s
IdentityFile /root/.ssh/id_code
`
configPath := "/root/.ssh/config"

if util.FileExists(configPath) {
if err := os.Remove(configPath); err != nil {
return err
}
}

config := fmt.Sprintf(c, url.Host)
err := ioutil.WriteFile(configPath, []byte(config), 0644)
if err != nil {
return err
}

return nil
}
1 change: 1 addition & 0 deletions server_config/server_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type Config struct {
Code string
CodeDir string
GitReference string
GitPrivateKey string
Ports []int
EnvironmentVariables map[string]string
}