Skip to content

Commit

Permalink
Remove -p option & web server support
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardobranco777 committed Sep 1, 2024
1 parent 29948b7 commit e129485
Show file tree
Hide file tree
Showing 14 changed files with 6 additions and 429 deletions.
14 changes: 0 additions & 14 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ jobs:
run: make test
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
- name: Selenium test
run: make selenium

shellcheck:
name: Shellcheck
Expand All @@ -61,15 +59,3 @@ jobs:
uses: ludeeus/action-shellcheck@master
with:
scandir: .

docker:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: docker/setup-buildx-action@v2
- name: test docker image
run: |
docker compose -f examples/docker-compose.yml --project-directory . build --pull
PASS=testing docker compose -f examples/docker-compose.yml --project-directory . up -d
sleep 10
test "$(curl -4k -u test:testing https://localhost:8443/test)" = "OK"
1 change: 0 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ RUN zypper addrepo https://download.opensuse.org/repositories/SUSE:/CA/openSUSE_
python3-cachetools \
python3-cryptography \
python3-Jinja2 \
python3-pyramid \
python3-python-dateutil \
python3-pytz \
python3-PyYAML && \
Expand Down
6 changes: 1 addition & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ pylint:

.PHONY: test
test:
@SKIP_SELENIUM=1 TZ=Europe/Berlin pytest --capture=sys -v --cov --cov-report term-missing

.PHONY: selenium
selenium:
@pytest tests/test_selenium.py
TZ=Europe/Berlin pytest --capture=sys -v --cov --cov-report term-missing

.PHONY: mypy
mypy:
Expand Down
17 changes: 1 addition & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Docker image available at `ghcr.io/ricardobranco777/cloudview:latest`
## Usage

```
usage: cloudview.py [-h] [-c CONFIG] [-f FORMAT] [-l {none,debug,info,warning,error,critical}] [-o {text,html,json}] [-p PORT] [-P {ec2,gce,azure_arm,openstack}] [-r]
usage: cloudview.py [-h] [-c CONFIG] [-f FORMAT] [-l {none,debug,info,warning,error,critical}] [-o {text,html,json}] [-P {ec2,gce,azure_arm,openstack}] [-r]
[-s {name,state,time}] [-S {error,migrating,normal,paused,pending,rebooting,reconfiguring,running,starting,stopped,stopping,suspended,terminated,unknown,updating}]
[-t TIME_FORMAT] [-v] [--version]
Expand All @@ -27,7 +27,6 @@ options:
logging level (default: error)
-o {text,html,json}, --output {text,html,json}
output type (default: text)
-p PORT, --port PORT run a web server on specified port (default: None)
-P {ec2,gce,azure_arm,openstack}, --providers {ec2,gce,azure_arm,openstack}
list only specified providers (default: None)
-r, --reverse reverse sort (default: False)
Expand Down Expand Up @@ -59,16 +58,6 @@ NOTES:

The [cloudview](scripts/cloudview) script scans `clouds.yaml` and environment variables to execute the proper `docker` command.

## To run the web server with Docker Compose:

If you have a TLS key pair, put the certificates in `cert.pem`, the private key in `key.pem` and the file containing the passphrase to the private key in `key.txt`. Then edit the [docker-compose.yml](examples/docker-compose.yml) file to mount the directory to `/etc/nginx/ssl` in the container like this: `- "/path/to/tls:/etc/nginx/ssl:ro"`. Set and export the `NGINX_HOST` environment variable with the FQDN of your host.

For HTTP Basic Authentication, create a file named `auth.htpasswd` in the same directory with the TLS key pair.

If you don't have a TLS key pair, a self-signed certificate and a random password for logging in will be generated. You can see the latter with `docker compose logs`. The user is `test`.

After running `docker compose build` & `docker compose up -d` you can browse to [https://localhost:8443](https://localhost:8443)

## Debugging

- For debugging you can set the `LIBCLOUD_DEBUG` environment variable to a path like `/dev/stderr`
Expand All @@ -79,7 +68,3 @@ After running `docker compose build` & `docker compose up -d` you can browse to
- [Azure](https://libcloud.readthedocs.io/en/stable/compute/drivers/azure_arm.html)
- [GCE](https://libcloud.readthedocs.io/en/stable/compute/drivers/gce.html)
- [Openstack](https://libcloud.readthedocs.io/en/stable/compute/drivers/openstack.html)

## Similar projects

- [public cloud watch](https://github.com/SUSE/pcw/)
97 changes: 1 addition & 96 deletions cloudview/cloudview.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,10 @@
import logging
import sys
from concurrent.futures import ThreadPoolExecutor
from json import JSONEncoder
from io import StringIO
from operator import itemgetter
from typing import Any
from urllib.parse import urlencode, quote, unquote

from wsgiref.simple_server import make_server
from pyramid.view import view_config
from pyramid.config import Configurator
from pyramid.response import Response
from pyramid.request import Request

import yaml
from libcloud.compute.types import Provider, LibcloudError

Expand Down Expand Up @@ -102,49 +94,16 @@ def print_instances(client: CSP) -> None:
Output().info(instance)


def print_info() -> Response | None:
def print_info() -> None:
"""
Print information about instances
"""
clients = get_clients(config_file=args.config)
sys.stdout = StringIO() if args.port else sys.stdout
Output().header()
if len(clients) > 0:
with ThreadPoolExecutor(max_workers=len(clients)) as executor:
executor.map(print_instances, clients)
Output().footer()
if args.port:
response = sys.stdout.getvalue() # type: ignore
sys.stdout.close()
return response
return None


def handle_requests(request: Request) -> Response | None:
"""
Handle HTTP requests
"""
logging.info(request)
response = print_info()
return Response(response)


def test(request: Request | None = None) -> Response | None:
"""
Used for testing
"""
if request:
logging.info(request)
response = "OK"
return Response(response)
return None


def not_found() -> Response:
"""
Not found!
"""
return Response("Not found!", status=404)


def valid_elem(elem: str) -> bool:
Expand All @@ -159,50 +118,6 @@ def valid_elem(elem: str) -> bool:
)


@view_config(route_name="instance")
def handle_instance(request: Request) -> Response:
"""
Handle HTTP requests for instances
"""
logging.info(request)
provider = request.matchdict["provider"]
cloud = request.matchdict["cloud"]
instance_id = request.matchdict["instance_id"]
if (
provider not in PROVIDERS
or not valid_elem(cloud)
or not valid_elem(instance_id)
):
return not_found()
client = list(get_clients(config_file=args.config, provider=provider, cloud=cloud))[
0
]
if client is None:
return not_found()
if client is not None:
info = client.get_instance(instance_id, **request.params)
if info is None:
return not_found()
response = JSONEncoder(default=str, indent=4, sort_keys=True).encode(info.extra)
return Response(response, content_type="application/json; charset=utf-8")


def web_server() -> None:
"""
Setup the WSGI server
"""
with Configurator() as config:
config.add_route("handle_requests", "/")
config.add_view(handle_requests, route_name="handle_requests")
config.add_route("test", "/test")
config.add_view(test, route_name="test")
config.add_route("instance", "instance/{provider}/{cloud}/{instance_id}")
config.scan()
app = config.make_wsgi_app()
server = make_server("0.0.0.0", args.port, app)
server.serve_forever()


def parse_args() -> argparse.Namespace:
"""
Parse command line options
Expand Down Expand Up @@ -233,9 +148,6 @@ def parse_args() -> argparse.Namespace:
choices=["text", "html", "json"],
help="output type",
)
argparser.add_argument(
"-p", "--port", type=port_number, help="run a web server on specified port"
)
argparser.add_argument(
"-P",
"--providers",
Expand Down Expand Up @@ -309,14 +221,7 @@ def main() -> None:
if args.verbose:
keys |= {"id": ""}

if args.port:
args.output = "html"
Output(type=args.output.lower(), keys=keys, refresh_seconds=600)

if args.port:
web_server()
sys.exit(1)

print_info()


Expand Down
23 changes: 0 additions & 23 deletions examples/docker-compose.yml

This file was deleted.

16 changes: 0 additions & 16 deletions nginx/Dockerfile

This file was deleted.

51 changes: 0 additions & 51 deletions nginx/entrypoint.sh

This file was deleted.

Binary file removed nginx/favicon.ico
Binary file not shown.
59 changes: 0 additions & 59 deletions nginx/site.conf.template

This file was deleted.

1 change: 0 additions & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ apache-libcloud
cachetools
cryptography
Jinja2
pyramid
python-dateutil
pytz
PyYAML
Loading

0 comments on commit e129485

Please sign in to comment.