From 5ff6702d9cdfc5eb0494b81960d072a3655a8f93 Mon Sep 17 00:00:00 2001 From: Akilan Selvacoumar Date: Sun, 8 Aug 2021 00:21:44 +0400 Subject: [PATCH] added functionality to trigger ansibles from cli --- client/TrackContainers.go | 18 +++++ client/trackcontainers/trackcontainers.json | 41 +++++++++++ cmd/action.go | 17 +++++ cmd/flags.go | 38 ++++++---- plugin/plugin.go | 37 ++++++++++ plugin/plugin_test.go | 44 +++++++++++- .../docker-ubuntu-sshd-x11/Dockerfile | 61 ++++++++++++++++ .../docker-ubuntu-sshd-x11/README.md | 72 +++++++++++++++++++ .../docker-ubuntu-sshd-x11/description.txt | 1 + .../docker-ubuntu-sshd-x11/ports.json | 16 +++++ .../docker-ubuntu-sshd-x11/scripts/start | 14 ++++ .../containers/docker-ubuntu-sshd/ports.json | 12 ++++ 12 files changed, 354 insertions(+), 17 deletions(-) create mode 100644 server/docker/containers/docker-ubuntu-sshd-x11/Dockerfile create mode 100644 server/docker/containers/docker-ubuntu-sshd-x11/README.md create mode 100644 server/docker/containers/docker-ubuntu-sshd-x11/description.txt create mode 100644 server/docker/containers/docker-ubuntu-sshd-x11/ports.json create mode 100644 server/docker/containers/docker-ubuntu-sshd-x11/scripts/start diff --git a/client/TrackContainers.go b/client/TrackContainers.go index 8b5ef02..c37c78c 100644 --- a/client/TrackContainers.go +++ b/client/TrackContainers.go @@ -144,4 +144,22 @@ func ReadTrackContainers(filename string) (*TrackContainers, error) { } return c, nil +} + +// GetContainerInformation gets information about container based on +// container ID provided +func GetContainerInformation(ID string) (*TrackContainer, error) { + // Getting the current containers + err, CurrentContainers := ViewTrackedContainers() + if err != nil { + return nil, err + } + // Iterating through all tracked containers to get the container information + // of the ID passed through the function parameter + for _, container := range CurrentContainers.TrackcontianerList { + if container.Container.ID == ID { + return &container, nil + } + } + return nil, errors.New("Container not found. ") } \ No newline at end of file diff --git a/client/trackcontainers/trackcontainers.json b/client/trackcontainers/trackcontainers.json index e69de29..918b120 100644 --- a/client/trackcontainers/trackcontainers.json +++ b/client/trackcontainers/trackcontainers.json @@ -0,0 +1,41 @@ +{ + "TrackContainer": [ + { + "ID": "858912730fb34f3cce38ef6d405e04519e44f50c2a496cd400e75aaa0def3c80", + "Container": { + "SSHUsername": "master", + "SSHPassword": "password", + "ID": "858912730fb34f3cce38ef6d405e04519e44f50c2a496cd400e75aaa0def3c80", + "TagName": "p2p-ubuntu", + "ImagePath": "/home/akilan/Documents/p2p-rendering-computation/server/docker/containers/docker-ubuntu-sshd/", + "Ports": { + "Port": [ + { + "PortName": "SSH", + "InternalPort": 22, + "Type": "tcp", + "ExternalPort": 44269, + "Description": "SSH Port" + }, + { + "PortName": "NoVNC", + "InternalPort": 5901, + "Type": "tcp", + "ExternalPort": 40725, + "Description": "NoVNC port" + }, + { + "PortName": "NoVNC", + "InternalPort": 6081, + "Type": "tcp", + "ExternalPort": 40053, + "Description": "NoVNC port" + } + ] + }, + "GPU": "false" + }, + "IpAddress": "0.0.0.0" + } + ] +} \ No newline at end of file diff --git a/cmd/action.go b/cmd/action.go index 429566e..ad43725 100644 --- a/cmd/action.go +++ b/cmd/action.go @@ -164,6 +164,23 @@ var CliAction = func(ctx *cli.Context) error { client.PrettyPrint(trackedContainers) } + //Executing plugin when the plugin flag is called + // --plugin + if ExecutePlugin != "" { + // The execute plugin requires the container ID provided when being executed + if ID != "" { + err := plugin.RunPluginCli(ExecutePlugin, ID) + if err != nil { + fmt.Println(err) + } else { + fmt.Println("Success") + } + } else { + fmt.Println("provide container ID") + } + + } + return nil } diff --git a/cmd/flags.go b/cmd/flags.go index fee32ff..a7dc82b 100644 --- a/cmd/flags.go +++ b/cmd/flags.go @@ -6,22 +6,23 @@ import ( // Variables declared for CLI var ( - AddServer string - ViewImages string - CreateVM string - ContainerName string - Ports string - Server bool - RemoveVM string - ID string - Specs string - GPU bool - UpdateServerList bool - ServerList bool - SetDefaultConfig bool - NetworkInterface bool - ViewPlugin bool + AddServer string + ViewImages string + CreateVM string + ContainerName string + Ports string + Server bool + RemoveVM string + ID string + Specs string + GPU bool + UpdateServerList bool + ServerList bool + SetDefaultConfig bool + NetworkInterface bool + ViewPlugin bool TrackedContainers bool + ExecutePlugin string ) var AppConfigFlags = []cli.Flag{ @@ -139,4 +140,11 @@ var AppConfigFlags = []cli.Flag{ EnvVars: []string{"TRACKED_CONTAINERS"}, Destination: &TrackedContainers, }, + &cli.StringFlag{ + Name: "ExecutePlugin", + Aliases: []string{"plugin"}, + Usage: "Plugin which needs to be executed", + EnvVars: []string{"EXECUTE_PLUGIN"}, + Destination: &ExecutePlugin, + }, } \ No newline at end of file diff --git a/plugin/plugin.go b/plugin/plugin.go index b59ea69..9ab9331 100644 --- a/plugin/plugin.go +++ b/plugin/plugin.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "git.sr.ht/~akilan1999/p2p-rendering-computation/client" "git.sr.ht/~akilan1999/p2p-rendering-computation/config" "gopkg.in/yaml.v2" "io/ioutil" @@ -230,3 +231,39 @@ func ReadHost(filename string) (*Host, error) { return c, nil } + +// RunPluginCli Runs ansible plugin based on plugin name and contianer name which +// is derived from the tracked containers file +func RunPluginCli(PluginName string, ContainerID string) error { + // Gets container information based on container ID + ContainerInformation, err := client.GetContainerInformation(ContainerID) + if err != nil { + return err + } + + // Setting Up IP's for which the plugins will be executed + var ExecuteIPs []*ExecuteIP + var ExecuteIP ExecuteIP + // Getting port no of SSH port + for _,port := range ContainerInformation.Container.Ports.PortSet { + if port.PortName == "SSH" { + ExecuteIP.SSHPortNo = fmt.Sprint(port.ExternalPort) + break + } + } + // Handle error if SSH port is not provided + if ExecuteIP.SSHPortNo == "" { + return errors.New("SSH port not found") + } + // IP address of the container + ExecuteIP.IPAddress = ContainerInformation.IpAddress + // Append IP to list of executor IP + ExecuteIPs = append(ExecuteIPs, &ExecuteIP) + // Run plugin to execute plugin + _, err = RunPlugin(PluginName,ExecuteIPs) + if err != nil { + return err + } + + return nil +} diff --git a/plugin/plugin_test.go b/plugin/plugin_test.go index c69fd38..979cbe2 100644 --- a/plugin/plugin_test.go +++ b/plugin/plugin_test.go @@ -2,6 +2,7 @@ package plugin import ( "fmt" + "git.sr.ht/~akilan1999/p2p-rendering-computation/client" "git.sr.ht/~akilan1999/p2p-rendering-computation/config" "git.sr.ht/~akilan1999/p2p-rendering-computation/server/docker" "strconv" @@ -30,7 +31,7 @@ func TestRunPlugin(t *testing.T) { //Test IP 1 configuration testip1.IPAddress = "0.0.0.0" - testip1.SSHPortNo = strconv.Itoa(container1.SSHPort) + testip1.SSHPortNo = strconv.Itoa(container1.Ports.PortSet[0].ExternalPort) // Create docker container and get SSH port container2 ,err := docker.BuildRunContainer(0,"false","") @@ -40,7 +41,7 @@ func TestRunPlugin(t *testing.T) { } //Test IP 2 configuration testip2.IPAddress = "0.0.0.0" - testip2.SSHPortNo = strconv.Itoa(container2.SSHPort) + testip2.SSHPortNo = strconv.Itoa(container2.Ports.PortSet[0].ExternalPort) testips = append(testips, &testip1) testips = append(testips, &testip2) @@ -89,4 +90,43 @@ func TestExecuteIP_ModifyHost(t *testing.T) { fmt.Println(err) t.Fail() } +} + +// Test to ensure the cli function runs as intended and executes +// the test ansible script +func TestRunPluginCli(t *testing.T) { + // Create docker container and get SSH port + container1 ,err := docker.BuildRunContainer(0,"false","") + if err != nil { + fmt.Println(err) + t.Fail() + } + + // Ensuring created container is the added to the tracked list + err = client.AddTrackContainer(container1, "0.0.0.0") + if err != nil { + fmt.Println(err) + t.Fail() + } + + // Running test Ansible script + err = RunPluginCli("TestAnsible", container1.ID) + if err != nil { + fmt.Println(err) + t.Fail() + } + + // Removes container information from the tracker IP addresses + err = client.RemoveTrackedContainer(container1.ID) + if err != nil { + fmt.Println(err) + t.Fail() + } + + // Removing container1 after Ansible is executed + err = docker.StopAndRemoveContainer(container1.ID) + if err != nil { + fmt.Println(err) + t.Fail() + } } \ No newline at end of file diff --git a/server/docker/containers/docker-ubuntu-sshd-x11/Dockerfile b/server/docker/containers/docker-ubuntu-sshd-x11/Dockerfile new file mode 100644 index 0000000..a3628ab --- /dev/null +++ b/server/docker/containers/docker-ubuntu-sshd-x11/Dockerfile @@ -0,0 +1,61 @@ +# ----------------------------------------------------------------------------- +# This is base image of Ubuntu LTS with SSHD service. +# +# Authors: Art567 +# Updated: Sep 20th, 2015 +# Require: Docker (http://www.docker.io/) +# ----------------------------------------------------------------------------- + + +# Base system is the latest LTS version of Ubuntu. +# from consol/ubuntu-xfce-vnc + +# due to dependency issues vnc is still work in progress +from ubuntu:20.04 + +# Switch to root user to install additional software +USER 0 + + +# Make sure we don't get notifications we can't answer during building. +env DEBIAN_FRONTEND noninteractive + + +# Prepare scripts and configs +add ./scripts/start /start + + +# Download and install everything from the repos. +run apt-get -q -y update; apt-get -q -y upgrade && \ + apt-get -q -y install sudo openssh-server && \ + mkdir /var/run/sshd + + +# Set root password +run echo 'root:password' >> /root/passwdfile + + +# Create user and it's password +run useradd -m -G sudo master && \ + echo 'master:password' >> /root/passwdfile + + +# Apply root password +run chpasswd -c SHA512 < /root/passwdfile && \ + rm /root/passwdfile + + +# Port 22 is used for ssh +expose 22 + + +# Assign /data as static volume. +volume ["/data"] + + +# Fix all permissions +run chmod +x /start + + +# Starting sshd +cmd ["/start"] diff --git a/server/docker/containers/docker-ubuntu-sshd-x11/README.md b/server/docker/containers/docker-ubuntu-sshd-x11/README.md new file mode 100644 index 0000000..ee62e54 --- /dev/null +++ b/server/docker/containers/docker-ubuntu-sshd-x11/README.md @@ -0,0 +1,72 @@ +Ubuntu LTS with SSH (Docker) +========= + +A Docker file to build a [docker.io] base container with Ubuntu LTS and a sshd service +[docker.io]: http://docker.io +Nice and easy way to get any server up and running using docker + + +Instructions +----------- + - Install Docker first. + Read [installation instructions] (http://docs.docker.io/en/latest/installation/). + + + - Clone this repository: + + `$ git clone https://github.com/art567/docker-ubuntu-sshd.git` + + + - Enter local directory: + + `$ cd docker-ubuntu-sshd` + + - Change passwords in Dockerfile: + + `$ vi Dockerfile` + + - Build the container: + + `$ sudo docker build -t art567/ubuntu .` + + + - Run the container: + + `$ sudo docker run -d=true --name=ubuntu --restart=always -p=2222:22 -v=/opt/data:/data art567/ubuntu /start` + + + - Your container will start and bind ssh to 2222 port. + + + - Find your local IP Address: + + `$ ifconfig` + + + - Connect to the SSH server: + + `$ ssh root@ -p 2222` + + + - If you don't want to deal with root: + + `$ ssh master@ -p 2222` + + +**VERY IMPORTANT!!!** +----------- + + **Don't forget to change passwords in Dockerfile before building image!** + + +Versions +----------- +Some branches represents Ubuntu versions. + +Branch `master` is always represented by latest Ubuntu LTS + + For example: + - 12.04 - Ubuntu 12.04 LTS + - 14.04 - Ubuntu 14.04 LTS + - 16.04 - Ubuntu 16.04 LTS + diff --git a/server/docker/containers/docker-ubuntu-sshd-x11/description.txt b/server/docker/containers/docker-ubuntu-sshd-x11/description.txt new file mode 100644 index 0000000..dc5b3fa --- /dev/null +++ b/server/docker/containers/docker-ubuntu-sshd-x11/description.txt @@ -0,0 +1 @@ +Simple Ubuntu 20.04 image \ No newline at end of file diff --git a/server/docker/containers/docker-ubuntu-sshd-x11/ports.json b/server/docker/containers/docker-ubuntu-sshd-x11/ports.json new file mode 100644 index 0000000..520565b --- /dev/null +++ b/server/docker/containers/docker-ubuntu-sshd-x11/ports.json @@ -0,0 +1,16 @@ +{ + "Port": [ + { + "PortName": "SSH", + "InternalPort": 22, + "Type": "tcp", + "Description": "SSH Port" + }, + { + "PortName": "VNC", + "InternalPort": 5900, + "Type": "tcp", + "Description": "VNC port" + } + ] +} \ No newline at end of file diff --git a/server/docker/containers/docker-ubuntu-sshd-x11/scripts/start b/server/docker/containers/docker-ubuntu-sshd-x11/scripts/start new file mode 100644 index 0000000..3c9caa9 --- /dev/null +++ b/server/docker/containers/docker-ubuntu-sshd-x11/scripts/start @@ -0,0 +1,14 @@ +#!/bin/bash +# ----------------------------------------------------------------------------- +# docker-ubuntu-sshd /start script +# +# Authors: Art567 +# Updated: Sep 20th, 2015 +# ----------------------------------------------------------------------------- + + +# Run OpenSSH server in daemon mode +/usr/sbin/sshd -D + +# Running x11 server with password + diff --git a/server/docker/containers/docker-ubuntu-sshd/ports.json b/server/docker/containers/docker-ubuntu-sshd/ports.json index fe41888..e24ff6a 100644 --- a/server/docker/containers/docker-ubuntu-sshd/ports.json +++ b/server/docker/containers/docker-ubuntu-sshd/ports.json @@ -5,6 +5,18 @@ "InternalPort": 22, "Type": "tcp", "Description": "SSH Port" + }, + { + "PortName": "NoVNC", + "InternalPort": 5901, + "Type": "tcp", + "Description": "NoVNC port" + }, + { + "PortName": "NoVNC", + "InternalPort": 6081, + "Type": "tcp", + "Description": "NoVNC port" } ] } \ No newline at end of file