From 8ba2cee45f88c64d096dbbb29e73741403f747d1 Mon Sep 17 00:00:00 2001 From: Ganesh Chandrasekaran Date: Tue, 12 Feb 2019 15:01:25 +0900 Subject: [PATCH] Base for saltstack server Issue-ID: CCSDK-1048 Change-Id: Ic177428fd9fb111919f4bfbd0978464a19ac75ab Signed-off-by: Ganesh Chandrasekaran --- pom.xml | 1 + saltstack-server/README.md | 331 +++++++++++++++++++++ saltstack-server/pom.xml | 138 +++++++++ saltstack-server/src/main/docker/Dockerfile | 13 + .../src/main/vagrant/Vagrantfile-sample | 69 +++++ .../src/main/vagrant/saltstack_sample_sls-2.yml | 34 +++ .../src/main/vagrant/saltstact_sample_sls.yml | 26 ++ 7 files changed, 612 insertions(+) create mode 100644 saltstack-server/README.md create mode 100644 saltstack-server/pom.xml create mode 100644 saltstack-server/src/main/docker/Dockerfile create mode 100644 saltstack-server/src/main/vagrant/Vagrantfile-sample create mode 100644 saltstack-server/src/main/vagrant/saltstack_sample_sls-2.yml create mode 100644 saltstack-server/src/main/vagrant/saltstact_sample_sls.yml diff --git a/pom.xml b/pom.xml index 851307b6..85ed8a53 100644 --- a/pom.xml +++ b/pom.xml @@ -26,6 +26,7 @@ dgbuilder dgbuilder-docker ansible-server + saltstack-server diff --git a/saltstack-server/README.md b/saltstack-server/README.md new file mode 100644 index 00000000..69e47ec4 --- /dev/null +++ b/saltstack-server/README.md @@ -0,0 +1,331 @@ +''' +/*- +* ============LICENSE_START======================================================= +* ONAP : CCSDK +* ================================================================================ +* Copyright (C) 2018 Samsung Electronics. All rights reserved. +* ================================================================================ +* +* ============================================================================= +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +* +* +* ============LICENSE_END========================================================= +*/ +''' +USING VAGRANT for CREATING SALTSTACK SERVER + +a. The SaltStack server should have it’s SSH enabled. +b. Via ssh user account we should have the access to run saltstack command (here we will see how to enable root access via ssh and connect to server via root user). +============ +INSTALLATION: Saltstack DEMO Environment creation: +============ + +1, Install VirtualBox. +2, Install Vagrant. +3, Download https://github.com/UtahDave/salt-vagrant-demo. You can use git or download a zip of the project directly from GitHub (sample Vagrant attached). +4, Extract the zip file you downloaded, and then open a command prompt to the extracted directory. +5, Run vagrant up to start the demo environment: vagrant up + After Vagrant ups (~10 minutes) and you are back at the command prompt, you are ready to continue. + More info: https://docs.saltstack.com/en/getstarted/fundamentals/ + +============ +Configuration: Sample Saltstack server execution configuration requirement. +============ +1, login to Master Saltstack server node: +"sudo vi /etc/ssh/sshd_config" and SET the following +PermitEmptyPasswords yes +PermitRootLogin yes + +SAVE and close. + +2, Run: "sudo passwd root" +and set the root password. +Then run: "sudo reboot" + +3, On the host machine, open the virtual box set a port forwarding to the master server for 2222 -> 22 +This will redirect messages to host machine to the Vagarant Master server. + +============ +TESTING: Sample Saltstack server command execution. +============ + + @Test + public void reqExecCommand_shouldSetSuccessReal() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + + params.put("HostName", "127.0.0.1"); + params.put("Port", "22"); + params.put("User", "sdn"); + params.put("Password", "foo"); + params.put("Id", "test1"); + params.put("Cmd", "ls -l"); + params.put("SlsExec", "false"); + params.put("Timeout", "12000"); + adapter = new SaltstackAdapterImpl(); + try { + adapter.reqExecCommand(params, svcContext); + String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); + TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); + assertEquals("200", status); + assertEquals(TestId, "test1"); + } catch (Exception e){ + //if local ssh is not enabled + System.out.print(e.getMessage()); + } + } + + @Test + public void reqExecCommand_shouldSetSuccessRealSLSCommand() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + + params.put("HostName", ""); + params.put("Port", "2222"); + params.put("User", "root"); + params.put("Password", "vagrant"); + params.put("Id", "test1"); + params.put("Cmd", "salt '*' test.ping --out=json --static"); + params.put("SlsExec", "false"); + params.put("Timeout", "12000"); + + adapter = new SaltstackAdapterImpl(); + try { + adapter.reqExecCommand(params, svcContext); + String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); + TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); + assertEquals("200", status); + assertEquals(TestId, "test1"); + TestId = svcContext.getAttribute("test1.minion1"); + assertEquals(TestId, "true"); + } catch (Exception e){ + //if saltstack ssh IP is not enabled + System.out.print(e.getMessage()); + } + } + + @Test + public void reqExecCommand_shouldSetSuccessRealCommand() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + + params.put("HostName", ""); + params.put("Port", "2222"); + params.put("User", "root"); + params.put("Password", "vagrant"); + params.put("Id", "test1"); + params.put("Cmd", "cd /srv/salt/; salt '*' state.apply vim --out=json --static"); + params.put("SlsExec", "true"); + params.put("Timeout", "12000"); + + adapter = new SaltstackAdapterImpl(); + try { + adapter.reqExecCommand(params, svcContext); + String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); + TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); + assertEquals("200", status); + assertEquals(TestId, "test1"); + } catch (Exception e){ + //if saltstack ssh IP is not enabled + System.out.print(e.getMessage()); + } + } + + @Test + public void reqExecCommand_shouldSetSuccessRealSSL() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + + params.put("HostName", ""); + params.put("Port", "2222"); + params.put("User", "root"); + params.put("Password", "vagrant"); + params.put("Id", "test1"); + params.put("SlsName", "vim"); + params.put("Timeout", "12000"); + params.put("NodeList", "minion1"); + + adapter = new SaltstackAdapterImpl(); + try { + adapter.reqExecSLS(params, svcContext); + String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); + TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); + assertEquals("200", status); + assertEquals(TestId, "test1"); + } catch (Exception e){ + //if saltstack ssh IP is not enabled + System.out.print(e.getMessage()); + } + } + + @Test + public void reqExecCommand_shouldSetSuccessEnvParam() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + + params.put("HostName", ""); + params.put("Port", "2222"); + params.put("User", "root"); + params.put("Password", "vagrant"); + params.put("Id", "test1"); + params.put("SlsName", "vim"); + params.put("Timeout", "12000"); + params.put("NodeList", "minion1"); + params.put("EnvParameters", "{\"exclude\": bar*}"); + + adapter = new SaltstackAdapterImpl(); + try { + adapter.reqExecSLS(params, svcContext); + String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); + TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); + assertEquals("200", status); + assertEquals(TestId, "test1"); + } catch (Exception e){ + //if saltstack ssh IP is not enabled + System.out.print(e.getMessage()); + } + } + + @Test + public void reqExecCommand_shouldSetSuccessFileParam() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + + params.put("HostName", ""); + params.put("Port", "2222"); + params.put("User", "root"); + params.put("Password", "vagrant"); + params.put("Id", "test1"); + params.put("SlsName", "vim"); + params.put("Timeout", "12000"); + params.put("NodeList", "minion1"); + params.put("EnvParameters", "{\"exclude\": \"bar,baz\"}"); + params.put("FileParameters", "{\"config.txt\":\"db_ip=10.1.1.1, sip_timer=10000\"}"); + + adapter = new SaltstackAdapterImpl(); + try { + adapter.reqExecSLS(params, svcContext); + String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); + TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); + assertEquals("200", status); + assertEquals(TestId, "test1"); + } catch (Exception e){ + //if saltstack ssh IP is not enabled + System.out.print(e.getMessage()); + } + } + + @Test + public void reqExecCommand_shouldSetSuccessPillarParam() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + + params.put("HostName", ""); + params.put("Port", "2222"); + params.put("User", "root"); + params.put("Password", "vagrant"); + params.put("Id", "test1"); + params.put("SlsName", "vim"); + params.put("Timeout", "12000"); + params.put("NodeList", "minion1"); + params.put("EnvParameters", "{\"exclude\": \"bar,baz\", \"pillar\":\"'{\\\"foo\\\": \\\"bar\\\"}'\"}"); + params.put("FileParameters", "{\"config.txt\":\"db_ip=10.1.1.1, sip_timer=10000\"}"); + + adapter = new SaltstackAdapterImpl(); + try { + adapter.reqExecSLS(params, svcContext); + String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); + TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); + assertEquals("200", status); + assertEquals(TestId, "test1"); + } catch (Exception e){ + //if saltstack ssh IP is not enabled + System.out.print(e.getMessage()); + } + } + + @Test + public void reqExecCommand_shouldSetSuccessMultiFileParam() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + + params.put("HostName", ""); + params.put("Port", "2222"); + params.put("User", "root"); + params.put("Password", "vagrant"); + params.put("Id", "test1"); + params.put("SlsName", "vim"); + params.put("Timeout", "12000"); + params.put("NodeList", "minion1"); + params.put("EnvParameters", "{\"exclude\": bar*}"); + params.put("FileParameters", "{\"config.txt\":\"db_ip=10.1.1.1, sip_timer=10000\" , \"config-tep.txt\":\"db_ip=10.1.1.1, sip_timer=10000\"}"); + + adapter = new SaltstackAdapterImpl(); + try { + adapter.reqExecSLS(params, svcContext); + String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); + TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); + assertEquals("200", status); + assertEquals(TestId, "test1"); + } catch (Exception e){ + //if saltstack ssh IP is not enabled + System.out.print(e.getMessage()); + } + } + + @Test + public void reqExecCommand_shouldSetSuccessSSLFile() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + + params.put("HostName", ""); + params.put("Port", "2222"); + params.put("User", "root"); + params.put("Password", "vagrant"); + params.put("Id", "test1"); + params.put("Timeout", "12000"); + params.put("NodeList", "minion1"); + params.put("SlsFile", "src/test/resources/config.sls"); + + adapter = new SaltstackAdapterImpl(); + try { + adapter.reqExecSLSFile(params, svcContext); + String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); + TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); + assertEquals("200", status); + assertEquals(TestId, "test1"); + } catch (Exception e){ + //if saltstack ssh IP is not enabled + System.out.print(e.getMessage()); + } + } + + @Test + public void reqExecCommand_shouldSetSuccessSSLFileMultiFileParam() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + + params.put("HostName", ""); + params.put("Port", "2222"); + params.put("User", "root"); + params.put("Password", "vagrant"); + params.put("Id", "test1"); + params.put("Timeout", "12000"); + params.put("NodeList", "minion1"); + params.put("SlsFile", "src/test/resources/config.sls"); + params.put("EnvParameters", "{\"exclude\": bar, \"pillar\":\"'{\\\"foo\\\": \\\"bar\\\"}'\"}"); + params.put("FileParameters", "{\"config.txt\":\"db_ip=10.1.1.1, sip_timer=10000\" , \"config-tep.txt\":\"db_ip=10.1.1.1, sip_timer=10000\"}"); + + adapter = new SaltstackAdapterImpl(); + try { + adapter.reqExecSLSFile(params, svcContext); + String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); + TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); + assertEquals("200", status); + assertEquals(TestId, "test1"); + } catch (Exception e){ + //if saltstack ssh IP is not enabled + System.out.print(e.getMessage()); + } + } \ No newline at end of file diff --git a/saltstack-server/pom.xml b/saltstack-server/pom.xml new file mode 100644 index 00000000..019fcef7 --- /dev/null +++ b/saltstack-server/pom.xml @@ -0,0 +1,138 @@ + + + 4.0.0 + + + org.onap.ccsdk.distribution + distribution-root + 0.4.1-SNAPSHOT + + + saltstack-server + 0.4.1-SNAPSHOT + pom + + ccsdk-distribution :: saltstack-server + Creates base saltstack-server Docker container + + + onap/ccsdk-saltstack-server-image + ${project.version} + + + + + + + org.codehaus.groovy.maven + gmaven-plugin + + + validate + + execute + + + + println project.properties['ccsdk.project.version']; + def versionArray; + if ( project.properties['ccsdk.project.version'] != null ) { + versionArray = project.properties['ccsdk.project.version'].split('\\.'); + } + + if (project.properties['ccsdk.project.version'].endsWith("-SNAPSHOT")) + { + project.properties['project.docker.latesttag.version']=versionArray[0] + '.' + versionArray[1] + "-STAGING-latest"; + } else { + project.properties['project.docker.latesttag.version']=versionArray[0] + '.' + versionArray[1] + "-STAGING-latest"; + } + + println 'New Tag for docker:' + + project.properties['project.docker.latesttag.version']; + + + + + + + + maven-resources-plugin + 2.6 + + + copy-dockerfile + + copy-resources + + validate + + ${basedir}/target/docker-stage + + + src/main/docker + + Dockerfile + + true + + + + + + + + + + + + docker + + + + io.fabric8 + docker-maven-plugin + 0.26.0 + false + + + + + ${image.name} + + try + ${basedir}/target/docker-stage + Dockerfile + + ${project.version} + ${project.version}-STAGING-${maven.build.timestamp} + ${project.docker.latesttag.version} + + + + + + + + generate-images + generate-sources + + build + + + + + push-images + deploy + + build + push + + + + + + + + + + diff --git a/saltstack-server/src/main/docker/Dockerfile b/saltstack-server/src/main/docker/Dockerfile new file mode 100644 index 00000000..f2e486eb --- /dev/null +++ b/saltstack-server/src/main/docker/Dockerfile @@ -0,0 +1,13 @@ +FROM centos:7 + + +RUN yum clean all && \ + yum install -y yum install epel-release && \ + yum install -y https://repo.saltstack.com/yum/redhat/salt-repo-latest-1.el7.noarch.rpm && \ + yum update -y && \ + yum install -y sudo git tmux vim salt-master salt-minion && \ + yum clean all + +EXPOSE 4505 4506 + +CMD /usr/bin/salt-master -d; /bin/bash \ No newline at end of file diff --git a/saltstack-server/src/main/vagrant/Vagrantfile-sample b/saltstack-server/src/main/vagrant/Vagrantfile-sample new file mode 100644 index 00000000..5fbcfbb7 --- /dev/null +++ b/saltstack-server/src/main/vagrant/Vagrantfile-sample @@ -0,0 +1,69 @@ +# -*- mode: ruby -*- +# vi: set ft=ruby : + +# Vagrantfile API/syntax version. Don't touch unless you know what you're doing! +VAGRANTFILE_API_VERSION = "2" + +Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| + os = "bento/ubuntu-16.04" + net_ip = "192.168.50" + + config.vm.define :master, primary: true do |master_config| + master_config.vm.provider "virtualbox" do |vb| + vb.memory = "2048" + vb.cpus = 1 + vb.name = "master" + end + master_config.vm.box = "#{os}" + master_config.vm.host_name = 'saltmaster.local' + master_config.vm.network "private_network", ip: "#{net_ip}.10" + master_config.vm.synced_folder "saltstack/salt/", "/srv/salt" + master_config.vm.synced_folder "saltstack/pillar/", "/srv/pillar" + + master_config.vm.provision :salt do |salt| + salt.master_config = "saltstack/etc/master" + salt.master_key = "saltstack/keys/master_minion.pem" + salt.master_pub = "saltstack/keys/master_minion.pub" + salt.minion_key = "saltstack/keys/master_minion.pem" + salt.minion_pub = "saltstack/keys/master_minion.pub" + salt.seed_master = { + "minion1" => "saltstack/keys/minion1.pub", + "minion2" => "saltstack/keys/minion2.pub" + } + + salt.install_type = "stable" + salt.install_master = true + salt.no_minion = true + salt.verbose = true + salt.colorize = true + salt.bootstrap_options = "-P -c /tmp" + end + end + + + [ + ["minion1", "#{net_ip}.11", "1024", os ], + ["minion2", "#{net_ip}.12", "1024", os ], + ].each do |vmname,ip,mem,os| + config.vm.define "#{vmname}" do |minion_config| + minion_config.vm.provider "virtualbox" do |vb| + vb.memory = "#{mem}" + vb.cpus = 1 + vb.name = "#{vmname}" + end + minion_config.vm.box = "#{os}" + minion_config.vm.hostname = "#{vmname}" + minion_config.vm.network "private_network", ip: "#{ip}" + + minion_config.vm.provision :salt do |salt| + salt.minion_config = "saltstack/etc/#{vmname}" + salt.minion_key = "saltstack/keys/#{vmname}.pem" + salt.minion_pub = "saltstack/keys/#{vmname}.pub" + salt.install_type = "stable" + salt.verbose = true + salt.colorize = true + salt.bootstrap_options = "-P -c /tmp" + end + end + end + end \ No newline at end of file diff --git a/saltstack-server/src/main/vagrant/saltstack_sample_sls-2.yml b/saltstack-server/src/main/vagrant/saltstack_sample_sls-2.yml new file mode 100644 index 00000000..b96773e1 --- /dev/null +++ b/saltstack-server/src/main/vagrant/saltstack_sample_sls-2.yml @@ -0,0 +1,34 @@ +# /*- +# * ============LICENSE_START======================================================= +# * ONAP : CCSDK +# * ================================================================================ +# * Copyright (C) 2018 Samsung Electronics. All rights reserved. +# * ================================================================================ +# * +# * ============================================================================= +# * Licensed under the Apache License, Version 2.0 (the "License"); +# * you may not use this file except in compliance with the License. +# * You may obtain a copy of the License at +# * +# * http://www.apache.org/licenses/LICENSE-2.0 +# * +# * Unless required by applicable law or agreed to in writing, software +# * distributed under the License is distributed on an "AS IS" BASIS, +# * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# * See the License for the specific language governing permissions and +# * limitations under the License. +# * +# * +# * ============LICENSE_END========================================================= +# */ + +my-vim: + git.latest: + - name: https://github.com/nbari/my-vim + - target: /usr/local/share/my-vim + - rev: master + - submodules: True + cmd.wait: + - name: 'cd /usr/local/share/my-vim; git submodule init; git submodule foreach git pull origin master; git submodule update' + - watch: + - git: my-vim \ No newline at end of file diff --git a/saltstack-server/src/main/vagrant/saltstact_sample_sls.yml b/saltstack-server/src/main/vagrant/saltstact_sample_sls.yml new file mode 100644 index 00000000..84cc917b --- /dev/null +++ b/saltstack-server/src/main/vagrant/saltstact_sample_sls.yml @@ -0,0 +1,26 @@ +# /*- +# * ============LICENSE_START======================================================= +# * ONAP : CCSDK +# * ================================================================================ +# * Copyright (C) 2018 Samsung Electronics. All rights reserved. +# * ================================================================================ +# * +# * ============================================================================= +# * Licensed under the Apache License, Version 2.0 (the "License"); +# * you may not use this file except in compliance with the License. +# * You may obtain a copy of the License at +# * +# * http://www.apache.org/licenses/LICENSE-2.0 +# * +# * Unless required by applicable law or agreed to in writing, software +# * distributed under the License is distributed on an "AS IS" BASIS, +# * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# * See the License for the specific language governing permissions and +# * limitations under the License. +# * +# * +# * ============LICENSE_END========================================================= +# */ + +vim: + pkg.installed -- 2.16.6