Skip to content

Commit

Permalink
feat: add simple agent skeleton
Browse files Browse the repository at this point in the history
This patch adds a simple skeleton code for the agent
  • Loading branch information
ccamacho committed Dec 15, 2020
1 parent e5a9d7c commit fcf7b35
Show file tree
Hide file tree
Showing 15 changed files with 586 additions and 1 deletion.
14 changes: 14 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ jobs:
sudo python3 -m pip install --upgrade virtualenv
sudo python3 -m pip install --upgrade setuptools
sudo python3 -m pip install -r ./test-requirements.txt
sudo python3 -m pip install tox shyaml
sudo python3 -m pip install twine
- name: Create collection build and publish
run: |
chmod +x ./tools/release.sh
Expand All @@ -46,6 +49,17 @@ jobs:
QUAY_KEY=${{ secrets.QUAY_KEY }} \
GALAXY_KEY=${{ secrets.GALAXY_KEY }} \
./tools/release.sh
- name: Create pypi package and publish
run: |
./tools/pypi_publish.sh -k ${{ secrets.PYPI_TOKEN }}
- name: Install latest Kubeinit agent version build locally if we published a new version
run: |
cd ./agent
if [ -d "./dist" ]
then
python3 -m pip install --force dist/*
kubeinit -v
fi
release_docs:
needs: release_sw
Expand Down
7 changes: 7 additions & 0 deletions agent/MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
include *.py
include *.yaml
include requirements.txt
recursive-include kubeinit *
global-exclude *pyc
global-exclude .project
global-exclude .pydevproject
21 changes: 21 additions & 0 deletions agent/kubeinit/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env python

"""
Copyright 2019 Kubeinit (kubeinit.com).
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.
"""

from pkg_resources import get_distribution

__version__ = get_distribution('kubeinit').version
147 changes: 147 additions & 0 deletions agent/kubeinit/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
#!/usr/bin/env python

"""
Copyright 2019 Kubeinit (kubeinit.com).
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.com/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.
"""


import logging
import logging.handlers
import os
import threading
from argparse import ArgumentParser

from kubeinit import __version__
from kubeinit.const import KUBEINIT_LOG_FILE
from kubeinit.get_banner import get_banner
from kubeinit.lib import connect_to_aas, connect_to_aas2


kubeinit_version = __version__

t1_stop = threading.Event()
t2_stop = threading.Event()

handler = logging.handlers.WatchedFileHandler(
KUBEINIT_LOG_FILE)
formatter = logging.Formatter(logging.BASIC_FORMAT)
handler.setFormatter(formatter)
root = logging.getLogger()
root.setLevel(os.environ.get("LOGLEVEL", "INFO"))
root.addHandler(handler)


def main():
"""
Application's entry point.
Here, application's settings are read from the command line,
environment variables and CRD. Then, retrieving and processing
of Kubernetes events are initiated.
"""
parser = ArgumentParser(
description='Kubeinit - CLI',
prog='kubeinit'
)

parser.add_argument(
'-v',
'--version',
action='version',
version='%(prog)s ' + kubeinit_version
)

parser.add_argument(
'-b',
'--banner',
action='store_true',
help='Print Kubeinit.com banner'
)

subparsers = parser.add_subparsers(title="Kubeinit subcommands",
dest="command",
help=("These are the options "
"supported: \n"
"The listen option will "
"watch for CRD events. "
"The run option will "
"execute the Kubeinit "
"actions against the cluster."))

parser_connect = subparsers.add_parser('connect', help=("CLI options to run the "
"Kubeinit actions."))

parser_connect.add_argument(
'-k',
'--key',
default="",
type=str,
help=("The connection key:"
"--extra-vars thisisakey"
"Defaults to: empty"))

subparsers.add_parser('status', help=("Show the connections status."))

parser_show = subparsers.add_parser('show',
help=("Get info from a connection."))

parser_show.add_argument(
"connection",
nargs='?',
default='',
type=str,
help=("Specify the connection to fetch details"))

args = parser.parse_args()

# print("Kubeinit called with the folowing parameters")
# print(parser.parse_args())

if args.banner:
print(get_banner())
exit()

try:
if (args.command == 'connect'):
print("We will watch for objects to process")
try:
print(args.key)
t1 = threading.Thread(target=connect_to_aas,
args=(t1_stop,))
t1.start()
t2 = threading.Thread(target=connect_to_aas2,
args=(t2_stop,))
t2.start()
except Exception as err:
print("Error: unable to start thread: " + err)
elif (args.command == 'status'):
print(u"\U0001F4DA" + " Listing the deployed actions"
" from the cluster.")
print(u"\U0001F4A1" + " For further information use:"
" kubeinit get <action_name> [--debug]")
exit()
elif (args.command == 'show'):
print(u"\U0001F4E4" + " Getting the details from"
" an specific action.")
print(args.connection)
exit()

except KeyboardInterrupt:
pass
except Exception as err:
raise RuntimeError('There is something wrong...' + err)

while not t2_stop.is_set() or not t1_stop.is_set():
pass
26 changes: 26 additions & 0 deletions agent/kubeinit/const.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env python

"""
Copyright 2019 Kubeinit (kubeinit.com).
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.
"""

import os
import tempfile

# Main logging file
KUBEINIT_LOG_FOLDER = tempfile.gettempdir()
KUBEINIT_LOG_FILE = os.path.join(KUBEINIT_LOG_FOLDER, '/tmp/kubeinit.log')

# Current branch
KUBEINIT_BRANCH = 'master'
42 changes: 42 additions & 0 deletions agent/kubeinit/get_banner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/env python

"""
Copyright 2019 Kubeinit (kubeinit.com).
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.
"""

from kubeinit import __version__

kubeinit_version = __version__


def get_banner():
"""
Get banner method.
This method prints
the kubeinit.com banner.
"""
banner = """
888 d8P 888 8888888 d8b 888
888 d8P 888 888 Y8P 888
888 d8P 888 888 888
888d88K 888 888 88888b. .d88b. 888 88888b. 888 888888
8888888b 888 888 888 "88b d8P Y8b 888 888 "88b 888 888
888 Y88b 888 888 888 888 88888888 888 888 888 888 888
888 Y88b Y88b 888 888 d88P Y8b. 888 888 888 888 Y88b.
888 Y88b "Y88888 88888P" "Y8888 8888888 888 888 888 "Y888
(kubeinit.com) agent version {}
""".format(kubeinit_version)
return banner
41 changes: 41 additions & 0 deletions agent/kubeinit/lib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env python

"""
Copyright 2019 Kubeinit (kubeinit.com).
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.
"""


def connect_to_aas(stop,
api_client=None):
"""
Watch for action with timeouts.
This method will listen for custom objects
that times out.
"""
while True:
return True


def connect_to_aas2(stop,
api_client=None):
"""
Watch for action with timeouts.
This method will listen for custom objects
that times out.
"""
while True:
return True
64 changes: 64 additions & 0 deletions agent/kubeinit/logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/usr/bin/env python

"""
Copyright 2019 Kubeinit (kubeinit.com).
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.
"""

import logging
import sys
from logging.handlers import TimedRotatingFileHandler

from kubeinit.const import KUBEINIT_LOG_FILE

FORMATTER = logging.Formatter("%(asctime)s - %(name)s - "
"%(levelname)s - %(message)s")


def get_console_handler():
"""
Get logger console handler.
This is a main component of the input for the controller
"""
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setFormatter(FORMATTER)
return console_handler


def get_file_handler():
"""
Get logger file handler.
This is a main component of the input for the controller
"""
file_handler = TimedRotatingFileHandler(
KUBEINIT_LOG_FILE, when='midnight')
file_handler.setFormatter(FORMATTER)
return file_handler


def get_logger(logger_name):
"""
Get logger.
This is a main component of the input for the controller
"""
logger = logging.getLogger(logger_name)
logger.setLevel(logging.DEBUG)
# We do not want to print in the console
# logger.addHandler(get_console_handler())
logger.addHandler(get_file_handler())
logger.propagate = False
return logger
14 changes: 14 additions & 0 deletions agent/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
pyyaml
requests
ansible
openshift
j2cli
jinja2
kubernetes
setuptools
PTable
rich
google-cloud
google-cloud-storage
google-cloud-firestore

Loading

0 comments on commit fcf7b35

Please sign in to comment.