added functionality to trigger ansibles from cli

This commit is contained in:
2021-08-08 00:21:44 +04:00
parent af4b129bee
commit 5ff6702d9c
12 changed files with 354 additions and 17 deletions

View File

@@ -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. ")
}

View File

@@ -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"
}
]
}

View File

@@ -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
}

View File

@@ -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,
},
}

View File

@@ -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
}

View File

@@ -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()
}
}

View File

@@ -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"]

View File

@@ -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@<ip-address> -p 2222`
- If you don't want to deal with root:
`$ ssh master@<ip-address> -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

View File

@@ -0,0 +1 @@
Simple Ubuntu 20.04 image

View File

@@ -0,0 +1,16 @@
{
"Port": [
{
"PortName": "SSH",
"InternalPort": 22,
"Type": "tcp",
"Description": "SSH Port"
},
{
"PortName": "VNC",
"InternalPort": 5900,
"Type": "tcp",
"Description": "VNC port"
}
]
}

View File

@@ -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

View File

@@ -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"
}
]
}