From 42e91bdf5b69af0fa5448867e5c96f78f85a03d3 Mon Sep 17 00:00:00 2001
From: svar <stephane@signal18.io>
Date: Mon, 27 Jan 2020 13:50:16 +0100
Subject: [PATCH] Replication Manager first push

---
 component/replication-manager/buildout.cfg |  43 +++++++
 software/repman/instance.cfg.in            | 128 +++++++++++++++++++++
 software/repman/software.cfg               |  41 +++++++
 software/repman/templates/config.toml.in   |  13 +++
 4 files changed, 225 insertions(+)
 create mode 100644 component/replication-manager/buildout.cfg
 create mode 100644 software/repman/instance.cfg.in
 create mode 100644 software/repman/software.cfg
 create mode 100644 software/repman/templates/config.toml.in

diff --git a/component/replication-manager/buildout.cfg b/component/replication-manager/buildout.cfg
new file mode 100644
index 000000000..29006a083
--- /dev/null
+++ b/component/replication-manager/buildout.cfg
@@ -0,0 +1,43 @@
+[buildout]
+extends =
+  ../../component/golang/buildout.cfg
+
+parts =
+  repman
+
+[gowork]
+golang  = ${golang1.12:location}
+install =
+buildflags =  -v --tags server --ldflags "-extldflags 'static' -w -s -X main.GoOS=linux -X main.GoArch=amd64 -X main.Version=2.1 -X main.FullVersion=$FULLVERSION -X main.Build=$(date +%FT%T%z) -X main.WithProvisioning=ON -X main.WithOpenSVC=OFF -X main.WithHaproxy=ON -X main.WithMaxscale=ON  -X main.WithMariadbshardproxy=ON -X  main.WithProxysql=ON -X  main.WithSphinx=ON -X main.WithArbitration=OFF -X main.WithArbitrationClient=ON -X main.WithMonitoring=ON -X main.WithHttp=ON -X main.WithBackup=ON -X main.WithMail=ON -X main.WithEnforce=ON -X main.WithDeprecate=ON"
+
+[gowork.goinstall]
+depends_gitfetch  =
+    ${git.signal18.io_signal18_repman:recipe}
+
+command = . ${gowork:env.sh} &&
+  cd ${git.signal18.io_signal18_repman:location}  &&
+  export GO111MODULE=on &&
+  export FULLVERSION=$(git describe --tags) &&
+  go build ${gowork:buildflags} -o ${gowork:bin}/replication-manager  &&
+  chmod -R u+w .
+
+#  export VERSION=$(git describe --abbrev=0 --tags) &&
+
+[git.signal18.io_signal18_repman]
+<= go-git-package
+go.importpath = github.com/signal18/replication-manager
+repository =  https://github.com/signal18/replication-manager
+branch = 2.1
+
+[repman]
+# revision and repository can be used to control which repman version is used
+depend = ${gowork:recipe}
+recipe  = collective.recipe.template
+# Do something useless to pass
+output  = ${buildout:bin-directory}/${:_buildout_section_name_}
+mode    = 0755
+input   = inline:
+    #!/bin/sh
+    ${gowork:bin}/replication-manager
+    --monitoring-basedir=system --monitoring-sharedir=software_release/src/share --http-root=software_release/src/dashboard monitor
+# --monitoring-datadir=$${directory:var}/lib --config=$${directory:etc}/config.toml --log-file=$${:logfile}
diff --git a/software/repman/instance.cfg.in b/software/repman/instance.cfg.in
new file mode 100644
index 000000000..0b6676a2f
--- /dev/null
+++ b/software/repman/instance.cfg.in
@@ -0,0 +1,128 @@
+#############################
+#
+# Deploy replication-manager instance
+#
+#############################
+[buildout]
+parts =
+  directory
+  publish-connection-parameter
+  instance-parameter
+  template-jinja2-base
+  replication-manager
+  repman-promise
+
+
+# Define egg directories to be the one from Software Release
+# (/opt/slapgrid/...)
+# Always the same.
+eggs-directory = ${buildout:eggs-directory}
+develop-eggs-directory = ${buildout:develop-eggs-directory}
+offline = true
+
+
+[instance-parameter]
+# Fetch arbitrary parameters defined by the user in SlapOS Master for his instance.
+# We use the slapconfiguration recipe with a few parameters (partition id,
+# computer id, certificate, etc).
+# It will then authenticate to SlapOS Master and fetch the instance parameters.
+# The parameters are accessible from $${instance-parameter:configuration.name-of-parameter}
+# Always the same. Just copy/paste.
+# See docstring of slapos.cookbook:slapconfiguration for more information.
+recipe = slapos.cookbook:slapconfiguration
+computer = $${slap-connection:computer-id}
+partition = $${slap-connection:partition-id}
+url = $${slap-connection:server-url}
+key = $${slap-connection:key-file}
+cert = $${slap-connection:cert-file}
+
+
+[slap-configuration]
+# apache-frontend reads from  a part named [slap-configuration]
+recipe = slapos.cookbook:slapconfiguration.serialised
+computer = $${slap-connection:computer-id}
+partition = $${slap-connection:partition-id}
+url = $${slap-connection:server-url}
+key = $${slap-connection:key-file}
+cert = $${slap-connection:cert-file}
+
+
+
+# Create all needed directories, depending on your needs
+[directory]
+recipe = slapos.cookbook:mkdirectory
+home = $${buildout:directory}
+etc = $${:home}/etc
+var = $${:home}/var
+# Executables put here will be started but not monitored (for startup scripts)
+script = $${:etc}/run/
+# Executables put here will be started and monitored (for daemons)
+service = $${:etc}/service
+# Executables put here will be launched after buildout has completed to see
+# if instance is running
+promise = $${:etc}/promise/
+# Path of the log directory used by our service (see [helloweb])
+log = $${:var}/log
+data = $${:var}/lib
+
+[replication-manager-credential]
+recipe = slapos.cookbook:generate.password
+username = admin
+bytes = 12
+
+[replication-manager]
+# repman service is listening on:
+# - global IPv6 address, and
+# - fixed port
+#
+# NOTE because every computer partition is allocated its own global IPv6
+# address, it is ok to fix the port - different hello-world instances will have
+# different IPv6 addresses and they all will be accessible at the same time.
+port = 10005
+ipv6 = $${instance-parameter:ipv6-random}
+# full URL - for convenience
+url = https://[$${:ipv6}]:$${:port}
+
+# the service will log here
+logfile = $${directory:log}/repman.log
+
+# Actual script that starts the service:
+# This recipe will try to "exec" the command-line after separating parameters.
+recipe = slapos.cookbook:wrapper
+command-line =
+   ${gowork:bin}/replication-manager  --monitoring-basedir=system --monitoring-sharedir=software_release/src/share --http-root=software_release/src/dashboard --monitoring-datadir=$${directory:var}/lib --config=$${directory:etc}/config.toml --log-file=$${:logfile} monitor
+
+# Put this shell script in the "etc/service" directory. Each executable of this
+# repository will be started and monitored by supervisord. If a service
+# exits/crashes, it will trigger a "bang" and cause a re-run of the instance.
+wrapper-path = $${directory:service}/replication-manager
+
+name = config.toml
+
+
+
+[template-jinja2-base]
+recipe = slapos.recipe.template:jinja2
+template = ${download-config-toml:destination}
+rendered =$${directory:etc}/config.toml
+extra-context =
+context =
+  key service_ipv6 replication-manager:ipv6
+  key service_user replication-manager-credential:username
+  key service_pwd replication-manager-credential:passwd
+# promise, that checks that helloweb service is alive
+[repman-promise]
+recipe = slapos.cookbook:check_port_listening
+path = $${directory:promise}/replication-manager-ipv6
+hostname= $${replication-manager:ipv6}
+port    = 10005
+
+
+# Publish all the parameters needed for the user to connect to the instance.
+# It can be anything: URL(s), password(s), or arbitrary parameters.
+# Here we'll just echo back the entered name as instance parameter
+[publish-connection-parameter]
+recipe = slapos.cookbook:publish
+url = $${replication-manager:url}
+username = $${replication-manager-credential:username}
+password = $${replication-manager-credential:passwd}
diff --git a/software/repman/software.cfg b/software/repman/software.cfg
new file mode 100644
index 000000000..a3ced0ea1
--- /dev/null
+++ b/software/repman/software.cfg
@@ -0,0 +1,41 @@
+[buildout]
+
+extends =
+  ../../stack/slapos.cfg
+  ../../stack/monitor/buildout.cfg
+  ../../component/replication-manager/buildout.cfg
+  ../../component/git/buildout.cfg
+
+parts =
+  slapos-cookbook
+  download-config-toml
+  template
+  repman
+  repman-repository
+
+
+[template]
+recipe = slapos.recipe.template
+url = ${:_profile_base_location_}/${:filename}
+mode = 0644
+output = ${buildout:directory}/template.cfg
+filename = instance.cfg.in
+
+
+[download-config-toml]
+recipe = slapos.recipe.build:download
+url = ${:_profile_base_location_}/templates/config.toml.in
+destination = ${buildout:directory}/config.toml.in
+mode = 644
+
+# Pin versions of eggs used that are not already pinned by stack/slapos.cfg
+[versions]
+slapos.recipe.template = 4.3
+rubygemsrecipe  = 0.2.2+slapos001
+
+[repman-repository]
+recipe  = slapos.recipe.build:gitclone
+git-executable = ${git:location}/bin/git
+location = ${buildout:directory}/src
+branch = 2.1
+repository = https://github.com/signal18/replication-manager
diff --git a/software/repman/templates/config.toml.in b/software/repman/templates/config.toml.in
new file mode 100644
index 000000000..cfc829b89
--- /dev/null
+++ b/software/repman/templates/config.toml.in
@@ -0,0 +1,13 @@
+[Default]
+api-bind = "[{{service_ipv6}}]"
+http-bind-address = "[{{service_ipv6}}]"
+monitoring-save-config = true
+api-https-bind = true
+api-credentials = "{{service_user}}:{{service_pwd}}"
+
+[cluster1]
+title = "cluster1"
+#db-servers-hosts = "[2408:5180:0:43::d2a5]:3306,[2409:5180:0:43::d2a5]:3306"
+#db-servers-prefered-master = "[2408:5180:0:43::d2a5]:3306"
+db-servers-hosts = ""
+db-servers-credential = "root:test"
-- 
2.30.9