Skip to content

Commit

Permalink
Merge pull request #1 from sunilsankar/feature/almalinux9
Browse files Browse the repository at this point in the history
Feature/almalinux9
  • Loading branch information
sunilsankar authored Jun 7, 2022
2 parents 27fa72b + 2b53842 commit 83459bc
Show file tree
Hide file tree
Showing 6 changed files with 200 additions and 36 deletions.
24 changes: 11 additions & 13 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
{
"files.associations": {
"*.yml": "ansible"
},
"prettier.printWidth": 120,
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"files.trimFinalNewlines": true,
"editor.renderWhitespace": "selection",
"cSpell.enabled": true,
"cSpell.words": [
"ansible"
]
}
"files.associations": {
"*.yml": "ansible"
},
"prettier.printWidth": 120,
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"files.trimFinalNewlines": true,
"editor.renderWhitespace": "selection",
"cSpell.enabled": true,
"cSpell.words": ["ansible"]
}
11 changes: 5 additions & 6 deletions Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@ Vagrant.configure(2) do |config|
config.ssh.insert_key = false
config.vm.synced_folder "./", "/vagrant"
config.vm.provision "shell", path: "tasks/install.sh"
config.vm.define "sandbox-8" do |node|
config.vm.define "sandbox-9" do |node|

node.vm.box = "almalinux/8"
node.vm.box = "almalinux/9"
node.vm.box_check_update = false
node.vm.box_version = "8.5.20220316"
node.vm.hostname = "sandbox-8"
node.vm.hostname = "sandbox-9"

node.vm.network "private_network", ip: "172.16.100.100"
node.vm.network "private_network", ip: "172.16.17.100"

node.vm.provider :virtualbox do |v|
v.name = "sanbox-8"
v.name = "sandbox-9"
v.memory = 4096
v.cpus = 2
end
Expand Down
167 changes: 167 additions & 0 deletions tasks/files/login.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
# Copyright (c) 2015-2018 Cisco Systems, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
"""Login Command Module."""

import logging
import os
import shlex
import subprocess

import click

from molecule import scenarios, util
from molecule.command import base

LOG = logging.getLogger(__name__)


class Login(base.Base):
"""
Login Command Class.
.. program:: molecule login
.. option:: molecule login
Target the default scenario.
.. program:: molecule login --scenario-name foo
.. option:: molecule login --scenario-name foo
Targeting a specific scenario.
.. program:: molecule login --host hostname
.. option:: molecule login --host hostname
Targeting a specific running host.
.. program:: molecule login --host hostname --scenario-name foo
.. option:: molecule login --host hostname --scenario-name foo
Targeting a specific running host and scenario.
.. program:: molecule --debug login
.. option:: molecule --debug login
Executing with `debug`.
.. program:: molecule --base-config base.yml login
.. option:: molecule --base-config base.yml login
Executing with a `base-config`.
.. program:: molecule --env-file foo.yml login
.. option:: molecule --env-file foo.yml login
Load an env file to read variables from when rendering
molecule.yml.
"""

def __init__(self, c):
"""Construct Login."""
super(Login, self).__init__(c)
self._pt = None

def execute(self, action_args=None):
"""
Execute the actions necessary to perform a `molecule login` and \
returns None.
:return: None
"""
c = self._config
if (not c.state.created) and c.driver.managed:
msg = "Instances not created. Please create instances first."
util.sysexit_with_message(msg)

hosts = [d["name"] for d in self._config.platforms.instances]
hostname = self._get_hostname(hosts)
self._get_login(hostname)

def _get_hostname(self, hosts):
hostname = self._config.command_args.get("host")
host_list = "\n".join(sorted(hosts))
if hostname is None:
if len(hosts) == 1:
hostname = hosts[0]
else:
msg = (
f"There are {len(hosts)} running hosts. Please specify "
"which with --host.\n\n"
f"Available hosts:\n{host_list}"
)
util.sysexit_with_message(msg)
match = [x for x in hosts if x.startswith(hostname)]
if len(match) == 0:
msg = (
f"There are no hosts that match '{hostname}'. You "
"can only login to valid hosts."
)
util.sysexit_with_message(msg)
elif len(match) != 1:
# If there are multiple matches, but one of them is an exact string
# match, assume this is the one they're looking for and use it.
if hostname in match:
match = [hostname]
else:
msg = (
f"There are {len(match)} hosts that match '{hostname}'. You "
"can only login to one at a time.\n\n"
f"Available hosts:\n{host_list}"
)
util.sysexit_with_message(msg)

return match[0]

def _get_login(self, hostname): # pragma: no cover
lines, columns = os.popen("stty size", "r").read().split()
login_options = self._config.driver.login_options(hostname)
login_options["columns"] = columns
login_options["lines"] = lines
login_cmd = self._config.driver.login_cmd_template.format(**login_options)

cmd = shlex.split(f"/usr/bin/env {login_cmd}")
subprocess.run(cmd)


@base.click_command_ex()
@click.pass_context
@click.option("--host", "-h", help="Host to access.")
@click.option(
"--scenario-name",
"-s",
default=base.MOLECULE_DEFAULT_SCENARIO_NAME,
help=f"Name of the scenario to target. ({base.MOLECULE_DEFAULT_SCENARIO_NAME})",
)
def login(ctx, host, scenario_name): # pragma: no cover
"""Log in to one instance."""
args = ctx.obj.get("args")
subcommand = base._get_subcommand(__name__)
command_args = {"subcommand": subcommand, "host": host}

s = scenarios.Scenarios(base.get_configs(args, command_args), scenario_name)
for scenario in s.all:
base.execute_subcommand(scenario.config, subcommand)
6 changes: 4 additions & 2 deletions tasks/files/motd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ echo "
===========================================================================
- Current user........: $USER
- CPU usage...........: $LOAD1, $LOAD5, $LOAD15 (1, 5, 15 min)
- Memory used.........: $MEMORY1 / $MEMORY2
- Memory used.........: $MEMORY1 MB / $MEMORY2 MB
- Swap in use.........: `free -m | tail -n 1 | awk '{print $3}'` MB
=========================================================================== "
===========================================================================
Sandbox Env to test and create ansible roles with molecule and podman
"
15 changes: 8 additions & 7 deletions tasks/files/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
cryptography<3.4.0
ansible==2.10.5
cryptography
ansible
passlib
selinux
lxml
ansible-lint==4.3.7
molecule==3.2.0
molecule_docker==0.3.3
molecule_podman==0.3.0
ansible-lint
molecule
molecule_docker
molecule_podman
pytest-testinfra
flake8
pylint
yamllint==1.25.0
yamllint
pyOpenSSL

13 changes: 5 additions & 8 deletions tasks/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@
dnf install epel-release -y
dnf clean all
dnf update -y
dnf config-manager --add-repo=https://download.docker.com/linux/centos/docker-ce.repo
dnf install sshpass podman python python3-pip gcc git cmake make gcc vim curl libnsl -y

dnf install sshpass docker-ce --nobest python3 python3-virtualenv gcc git cmake make gcc vim curl libnsl -y
pip install --upgrade pip setuptools
pip install --ignore-installed PyYAML
pip install -r /vagrant/tasks/files/requirements.txt

pip3 install --upgrade pip setuptools
pip3 install --ignore-installed PyYAML
pip3 install -r /vagrant/tasks/files/requirements.txt
systemctl start docker
systemctl enable docker
usermod -G docker vagrant
cp /vagrant/tasks/files/motd.sh /etc/profile.d/
\cp /vagrant/tasks/files/login.py /usr/local/lib/python3.9/site-packages/molecule/command/login.py

0 comments on commit 83459bc

Please sign in to comment.