Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autodeploy service for dev #749

Merged
merged 9 commits into from
Feb 4, 2019
32 changes: 2 additions & 30 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,6 @@ var (
once sync.Once
)

// ServiceConfig contains information related to services that the config knows about
type ServiceConfig struct {
URL string
Env map[string]string
Sid string
Hash string
}

// ServiceConfigWithKey contains information related to services that the config knows about and their key
type ServiceConfigWithKey struct {
*ServiceConfig
Key string
}

// Config contains all the configuration needed.
type Config struct {
Server struct {
Expand Down Expand Up @@ -63,17 +49,7 @@ type Config struct {
}
}

// You can add any attribute that will be a `ServiceConfig` type
// example: `Foo ServiceConfig`
// You can then access it with config.Service.Foo.Sid in order to access the Sid of this service.
// The services in this struct are automatically started when Core starts based on the URL and Env.
// Sid and Hash are populated only after the deployment.
// You need to initialize these services in the `New` function
// example: `c.Service.Foo = ServiceConfig{URL: "https://api.github.com/repos/mesg-foundation/service-ethereum-erc20/tarball/master"}`
// Also add it in the `Services()` function
// example: `[]*ServiceConfigWithKey{{&c.Service.Foo, "Foo"}}`
Service struct {
}
Service ServiceConfigGroup

Docker struct {
Socket string
Expand Down Expand Up @@ -103,6 +79,7 @@ func New() (*Config, error) {
c.Core.Database.ExecutionRelativePath = filepath.Join("database", "executions")
c.Docker.Core.Path = "/mesg"
c.Docker.Socket = "/var/run/docker.sock"
c.Service = c.getServiceConfigGroup()
return &c, nil
}

Expand Down Expand Up @@ -152,11 +129,6 @@ func (c *Config) Validate() error {
return nil
}

// Services returns all services that the configuration package is aware of
func (c *Config) Services() []*ServiceConfigWithKey {
return []*ServiceConfigWithKey{}
}

// DaemonEnv returns the needed environmental variable for the Daemon.
func (c *Config) DaemonEnv() map[string]string {
return map[string]string{
Expand Down
46 changes: 46 additions & 0 deletions config/services.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package config

// Default endpoints to access services. These endpoints are overritten by the build
// Use the following format for the variable name: "[Service]URL" (where Service is the name of the service capitalized)
// The service name should be the name of the directory inside `systemservices` but capitalized
// example:
// var (
// BarURL string
// )
var (
EthwalletURL string
)

// ServiceConfig contains information related to services that the config knows about
type ServiceConfig struct {
URL string
Env map[string]string
Sid string
Hash string
}

// ServiceConfigWithKey contains information related to services that the config knows about and their key
type ServiceConfigWithKey struct {
*ServiceConfig
Key string
}

// ServiceConfigGroup is the struct that contains all the `ServiceConfig` related to the config
type ServiceConfigGroup struct {
Ethwallet ServiceConfig
}

// getServiceConfigGroup return the config for all services.
// DO NOT USE STRING HERE but variable defined on top of this file `XxxURL` for config injection
func (c *Config) getServiceConfigGroup() ServiceConfigGroup {
return ServiceConfigGroup{
Ethwallet: ServiceConfig{URL: EthwalletURL},
}
}

// Services returns all services that the configuration package is aware of
func (c *Config) Services() []*ServiceConfigWithKey {
return []*ServiceConfigWithKey{
{&c.Service.Ethwallet, "Ethwallet"},
}
}
31 changes: 31 additions & 0 deletions dev-core
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,36 @@ MESG_LOG_FORCECOLORS=${MESG_LOG_FORCECOLORS:-false}
VERSION=local
LDFLAGS="-X 'github.com/mesg-foundation/core/version.Version=$VERSION'"
CLIFLAGS=""
MESG_SERVICE_SERVER="core-service-server"

function deleteServiceServer {
rm -rf tmp-systemservices
if docker service list | grep -q core-service-server; then
docker service rm $MESG_SERVICE_SERVER
fi
}

deleteServiceServer
mkdir -p tmp-systemservices
for i in systemservices/* ; do
if [ -d "$i" ]; then
echo "Creating archive for $i..."
cd $i
antho1404 marked this conversation as resolved.
Show resolved Hide resolved
file=$(basename "$i")
tar -czf ../../tmp-systemservices/$file.tar.gz *
varname="$(tr '[:lower:]' '[:upper:]' <<< ${file:0:1})${file:1}"
LDFLAGS+=" -X 'github.com/mesg-foundation/core/config.${varname}URL=http://$MESG_SERVICE_SERVER/$file.tar.gz'"
cd ../..
fi
done
echo "FROM nginx:alpine" >> tmp-systemservices/Dockerfile
echo "COPY . /usr/share/nginx/html" >> tmp-systemservices/Dockerfile
docker build tmp-systemservices -t $MESG_SERVICE_SERVER
docker service create -d \
-p 8080:8080 \
--name $MESG_SERVICE_SERVER \
--network core \
$MESG_SERVICE_SERVER

echo "compile cli and core"
GOOS=linux GOARCH=amd64 go build -o ./bin/core -ldflags="$LDFLAGS" core/main.go
Expand Down Expand Up @@ -51,6 +81,7 @@ trap onexit EXIT INT

function onexit {
./bin/cli stop
deleteServiceServer
}

if [[ "$MESG_LOG_FORCECOLORS" = true ]]; then
Expand Down