Skip to content

Commit

Permalink
improve lsvirtualenvs
Browse files Browse the repository at this point in the history
    - add Dockerfile linter
    - add new rake tasks for docker and docker hub
    - now app works perfectly from the container!
    - remove slack notifier
  • Loading branch information
vigo committed Feb 24, 2022
1 parent b4abdc1 commit a36810c
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 34 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: dockerfile

on: push

jobs:
linter:
runs-on: ubuntu-latest
steps:

- uses: actions/checkout@v2

- name: Lint Dockerfile
uses: hadolint/hadolint-action@master
with:
dockerfile: "Dockerfile"
12 changes: 0 additions & 12 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,3 @@ jobs:

- name: Build
run: go build -v .

- name: Slack Notify Success
if: success()
uses: cemkiy/action-slacker@master
env:
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
with:
channel: '#github'
username: 'github'
icon_url: 'https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png'
color: '#00FFAA'
title: ${{ github.repository }} -> ${{ github.sha }}
11 changes: 7 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
FROM golang:1.16-alpine AS builder
WORKDIR /go/src/github.com/vigo/lsvirtualenvs
COPY . .
RUN apk add --no-cache git
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o lsvirtualenvs .

FROM alpine:latest
RUN apk --no-cache add ca-certificates
RUN apk add --no-cache git=2.34.1-r0 \
ca-certificates=20211220-r0 \
&& CGO_ENABLED=0 GOOS=linux \
go build -a -installsuffix cgo -o lsvirtualenvs .

FROM alpine:3.15
COPY --from=builder /go/src/github.com/vigo/lsvirtualenvs/lsvirtualenvs /bin/lsvirtualenvs
ENTRYPOINT ["/bin/lsvirtualenvs"]
37 changes: 24 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
[![Build Status](https://travis-ci.org/vigo/lsvirtualenvs.svg?branch=main)](https://travis-ci.org/vigo/lsvirtualenvs)
![Go Build Status](https://github.com/vigo/lsvirtualenvs/actions/workflows/go.yml/badge.svg)
![GolangCI-Lint Status](https://github.com/vigo/lsvirtualenvs/actions/workflows/golang-lint.yml/badge.svg)
![Docker Status](https://github.com/vigo/lsvirtualenvs/actions/workflows/docker.yml/badge.svg)


# List Virtual Environments for `virtualenvwrapper`

Expand All @@ -15,15 +17,6 @@ information, I made this simple cli-tool with `golang`.

---

## Update

**2022-02-15**

- Add `LSVIRTUALENVS_COLOR_ALWAYS` environment variable check. Set `LSVIRTUALENVS_COLOR_ALWAYS=1` for
colored output all the time
- Add GolangCI-Lint checker


## Requirements

I’m assuming that you are already using [virtualenvwrapper][virtualenvwrapper]
Expand Down Expand Up @@ -134,16 +127,28 @@ of dockerized version of the application :)
Build:

```bash
$ docker build -t lsvirtualenvs .
$ docker build -t lsvirtualenvs:latest .
```

Run:

```bash
$ docker run -i -t lsvirtualenvs lsvirtualenvs
WORKON_HOME environment variable doesn't exists in your environment
$ docker run --read-only -v "${WORKON_HOME}":/venvs --env WORKON_HOME=/venvs lsvirtualenvs:latest
$ docker run --read-only -v "${WORKON_HOME}":/venvs --env WORKON_HOME=/venvs lsvirtualenvs:latest -h
```

If you run it from container, currently, it’s not possible to get python
versions of the existing environments.

$ docker run -i -t lsvirtualenvs lsvirtualenvs -h
---

## Docker (docker hub)

https://hub.docker.com/r/vigo/lsvirtualenvs/

```bash
# latest
docker run --read-only -v "${WORKON_HOME}":/venvs --env WORKON_HOME=/venvs vigo/lsvirtualenvs -h
```

---
Expand All @@ -162,6 +167,12 @@ rake test[verbose] # run tests

## Change Log

**2022-02-15**

- Add `LSVIRTUALENVS_COLOR_ALWAYS` environment variable check. Set `LSVIRTUALENVS_COLOR_ALWAYS=1` for
colored output all the time
- Add GolangCI-Lint checker

**2021-05-09**

- Add github action for go build status
Expand Down
22 changes: 21 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,14 @@ end
# docker
# -----------------------------------------------------------------------------
namespace :docker do
desc "Lint"
task :lint do
system "hadolint Dockerfile"
end

desc "Build"
task :build do
system "docker build . -t lsvirtualenvs"
system "docker build . -t lsvirtualenvs:latest"
end

desc "Delete image"
Expand All @@ -102,5 +107,20 @@ namespace :docker do
task :run do
system "docker run -i -t lsvirtualenvs:latest lsvirtualenvs -h"
end

desc "Build and push to docker hub (latest)"
task :build_and_push do
current_git_tag = "v#{Rake::Task['current_version'].execute.first.call}"

system %{
docker build -t vigo/lsvirtualenvs:latest . &&
echo "-> vigo/lsvirtualenvs:latest" &&
docker build -t vigo/lsvirtualenvs:#{current_git_tag} . &&
echo "-> vigo/lsvirtualenvs:#{current_git_tag}" &&
docker push vigo/lsvirtualenvs:latest &&
docker push vigo/lsvirtualenvs:#{current_git_tag} &&
echo "-> pushed both..."
}
end
end
# -----------------------------------------------------------------------------
20 changes: 16 additions & 4 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,27 +103,39 @@ func (c *CLIApplication) Run() error {
return errWorkonHomeEnvNotExists
}

insideContainer := false
if _, err := os.Stat("/.dockerenv"); err == nil {
insideContainer = true
}

files, err := ioutil.ReadDir(workonHome)
if err != nil {
return fmt.Errorf("read dir error %w", err)
}

listEnvs = make(map[string]string)

for _, file := range files {
if file.IsDir() {
c := make(chan []string)

go func(dirName string, c chan []string) {
pythonBin := workonHome + "/" + dirName + "/bin/python"
cmd := pythonBin + " --version 2>&1"

pyVersion, err := exec.Command("bash", "-c", cmd).Output()
if err == nil {
pyVersion = bytes.TrimSpace(pyVersion)
c <- []string{dirName, strings.Split(string(pyVersion), " ")[1]}
if insideContainer {
c <- []string{dirName, "n/a"}
} else {
pyVersion, err := exec.Command("bash", "-c", cmd).Output()
if err == nil {
pyVersion = bytes.TrimSpace(pyVersion)
c <- []string{dirName, strings.Split(string(pyVersion), " ")[1]}
}
}
}(file.Name(), c)

result := <-c

listEnvs[result[0]] = result[1]
}
}
Expand Down

0 comments on commit a36810c

Please sign in to comment.