-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
200 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package provider | ||
|
||
import ( | ||
"github.com/renproject/darknode-cli/darknode" | ||
"github.com/renproject/darknode-cli/util" | ||
"github.com/urfave/cli" | ||
) | ||
|
||
type providerSsh struct { | ||
user string | ||
hostname string | ||
priKeyPath string | ||
} | ||
|
||
func NewSsh(ctx *cli.Context) (Provider, error) { | ||
user := ctx.String("ssh-user") | ||
hostname := ctx.String("ssh-hostname") | ||
priKeyPath := ctx.String("ssh-private-key") | ||
|
||
return providerSsh{ | ||
user: user, | ||
hostname: hostname, | ||
priKeyPath: priKeyPath, | ||
}, nil | ||
} | ||
|
||
func (p providerSsh) Name() string { | ||
return NameSsh | ||
} | ||
|
||
func (p providerSsh) Deploy(ctx *cli.Context) error { | ||
name := ctx.String("name") | ||
tags := ctx.String("tags") | ||
|
||
latestVersion, err := util.LatestStableRelease() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Initialization | ||
network, err := darknode.NewNetwork(ctx.String("network")) | ||
if err != nil { | ||
return err | ||
} | ||
if err := initNode(name, tags, network); err != nil { | ||
return err | ||
} | ||
|
||
// Generate terraform config and start deploying | ||
if err := p.tfConfig(name, latestVersion); err != nil { | ||
return err | ||
} | ||
if err := runTerraform(name); err != nil { | ||
return err | ||
} | ||
return outputURL(name) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package provider | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"text/template" | ||
|
||
"github.com/renproject/darknode-cli/util" | ||
) | ||
|
||
type sshTerraform struct { | ||
Name string | ||
User string | ||
Hostname string | ||
PriKeyPath string | ||
ConfigPath string | ||
ServiceFile string | ||
LatestVersion string | ||
} | ||
|
||
func (p providerSsh) tfConfig(name, latestVersion string) error { | ||
tf := sshTerraform{ | ||
Name: name, | ||
User: p.user, | ||
Hostname: p.hostname, | ||
ConfigPath: fmt.Sprintf("~/.darknode/darknodes/%v/config.json", name), | ||
PriKeyPath: fmt.Sprintf(p.priKeyPath), | ||
ServiceFile: darknodeService, | ||
LatestVersion: latestVersion, | ||
} | ||
|
||
t, err := template.New("ssh").Parse(sshTemplate) | ||
if err != nil { | ||
return err | ||
} | ||
tfFile, err := os.Create(filepath.Join(util.NodePath(name), "main.tf")) | ||
if err != nil { | ||
return err | ||
} | ||
return t.Execute(tfFile, tf) | ||
} | ||
|
||
var sshTemplate = ` | ||
resource "null_resource" "darknode" { | ||
provisioner "remote-exec" { | ||
inline = [ | ||
"set -x", | ||
"until sudo apt update; do sleep 4; done", | ||
"sudo adduser darknode --gecos \",,,\" --disabled-password", | ||
"sudo rsync --archive --chown=darknode:darknode ~/.ssh /home/darknode", | ||
"until sudo apt-get -y update; do sleep 4; done", | ||
"until sudo apt-get -y upgrade; do sleep 4; done", | ||
"until sudo apt-get -y autoremove; do sleep 4; done", | ||
"until sudo apt-get install ufw; do sleep 4; done", | ||
"sudo ufw limit 22/tcp", | ||
"sudo ufw allow 18514/tcp", | ||
"sudo ufw allow 18515/tcp", | ||
"sudo ufw --force enable", | ||
"until sudo apt-get -y install jq; do sleep 4; done", | ||
] | ||
connection { | ||
host = "{{.Hostname}}" | ||
type = "ssh" | ||
user = "{{.User}}" | ||
private_key = file("{{.PriKeyPath}}") | ||
} | ||
} | ||
provisioner "file" { | ||
source = "{{.ConfigPath}}" | ||
destination = "$HOME/config.json" | ||
connection { | ||
host = "{{.Hostname}}" | ||
type = "ssh" | ||
user = "darknode" | ||
private_key = file("{{.PriKeyPath}}") | ||
} | ||
} | ||
provisioner "remote-exec" { | ||
inline = [ | ||
"set -x", | ||
"mkdir -p $HOME/.darknode/bin", | ||
"mkdir -p $HOME/.config/systemd/user", | ||
"mv $HOME/config.json $HOME/.darknode/config.json", | ||
"curl -sL https://www.github.com/renproject/darknode-release/releases/latest/download/darknode > ~/.darknode/bin/darknode", | ||
"chmod +x ~/.darknode/bin/darknode", | ||
"echo {{.LatestVersion}} > ~/.darknode/version", | ||
<<EOT | ||
echo "{{.ServiceFile}}" > ~/.config/systemd/user/darknode.service | ||
EOT | ||
, | ||
"loginctl enable-linger darknode", | ||
"systemctl --user enable darknode.service", | ||
"systemctl --user start darknode.service", | ||
] | ||
connection { | ||
host = "{{.Hostname}}" | ||
type = "ssh" | ||
user = "darknode" | ||
private_key = file("{{.PriKeyPath}}") | ||
} | ||
} | ||
} | ||
output "provider" { | ||
value = "ssh" | ||
} | ||
output "ip" { | ||
value = "{{.Hostname}}" | ||
}` |