Skip to content

Commit

Permalink
Merge pull request #16 from moebius-rex/1.2.0-patches
Browse files Browse the repository at this point in the history
1.2.0 patches
  • Loading branch information
moebius-rex authored May 7, 2021
2 parents 5bd51d5 + 28172a5 commit aced749
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 37 deletions.
16 changes: 12 additions & 4 deletions docker/DOCKER.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ The detailed steps below execute the following commands:

The project's `docker` subdirectory provides Docker files to build up to three Docker images, each one based on a different Linux distribution:

- Alpine 3.13, a container-only Linux distribution
- Alpine 3.13
- Fedora 33
- Ubuntu 20.04 LTS

Expand Down Expand Up @@ -180,9 +180,17 @@ The project comes with a small shell script in the project's home directory, `pr
% prime fedora bash # open bash shell in fedora container
```

Docker commands can also be chained together, for example, the following command stops all docker containers, deletes and then rebuilds all docker images, restarts containers and opens a bash shell in the alpine container:
Docker commands can also be chained together, for example, the following command stops all docker containers, deletes and then rebuilds all docker images, restarts docker containers and opens Alpine's default [Almquist](https://en.wikipedia.org/wiki/Almquist_shell) shell in the alpine container:

```bash
% prime down prune build up alpine bash
% prime down prune build up alpine sh
/prime #
```

#### Note
The actual shell that the `sh` command starts depends on which Linux distribution you're using:
- Alpine Linux starts `ash`, the [Almquist](https://en.wikipedia.org/wiki/Almquist_shell) shell
- Fedora Linux starts `bash`, the Bourne Again Shell
- Ubuntu Linux starts `sh`, the Bourne Shell (Ubuntu also includes `bash`)

#### Note
This project does not support Docker-in-Docker. When we're ready to implement CI, e.g., using Jenkins, we'll create sibling, not child, Docker containers, as outlined in [this blog post](https://jpetazzo.github.io/2015/09/03/do-not-use-docker-in-docker-for-ci/). Therefore, Docker is not installed in project Docker images, and all Docker-related files are excluded from Docker images by the project's `.dockerignore` file.
10 changes: 8 additions & 2 deletions docker/alpine.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ RUN apk upgrade
RUN apk add file less tree vim

# install project build & run toolchains
RUN apk add bash
RUN apk add gcc
RUN apk add go
RUN apk add g++
Expand All @@ -38,6 +37,13 @@ RUN apk del openjdk8
# copy project source files to image & remove generated files
WORKDIR /prime
COPY . .
RUN printf "\nexport PATH=\$PATH:/prime\n" >> ~/.bashrc

# add prime directory to PATH for any shell
ENV PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/prime
RUN printf "export PATH=\$PATH:/prime\n" > /etc/profile.d/prime.sh

# some distros don't include /usr/local/include
RUN if [ ! -e /usr/local/include ]; then mkdir /usr/local/include; fi

# complete project setup
RUN setup/setup.sh
11 changes: 10 additions & 1 deletion docker/fedora.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ RUN dnf install -y file less tree vim
# install project build & run toolchains
RUN dnf install -y gcc
RUN dnf install -y gcc-c++
RUN dnf install -y gccgo
RUN dnf install -y go
RUN dnf install -y make
RUN dnf install -y maven
Expand All @@ -33,5 +34,13 @@ RUN dnf install -y python3
# copy project source files to image & remove generated files
WORKDIR /prime
COPY . .
RUN printf "\nexport PATH=\$PATH:/prime\n" >> ~/.bashrc

# add prime directory to PATH for any shell
ENV PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/prime
RUN printf "export PATH=\$PATH:/prime\n" > /etc/profile.d/prime.sh

# some distros don't include /usr/local/include
RUN if [ ! -e /usr/local/include ]; then mkdir /usr/local/include; fi

# complete project setup
RUN setup/setup.sh
35 changes: 22 additions & 13 deletions docker/ubuntu.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,43 @@
FROM ubuntu:focal

# update package list & upgrade
RUN apt-get update -y
RUN apt-get upgrade -y
RUN apt update -y
RUN apt upgrade -y

# disable all interactive package installation dialogs while building the image
ARG DEBIAN_FRONTEND=noninteractive

# install utilities
RUN apt-get install -y file less tree vim
RUN apt install -y file less tree vim

# install project build & run toolchains
RUN apt-get install -y gcc
RUN apt-get install -y golang-go
RUN apt-get install -y g++
RUN apt-get install -y make
RUN apt-get install -y maven
RUN apt-get install -y nodejs
RUN apt-get install -y python3
RUN apt install -y gcc
RUN apt install -y gccgo-go
RUN apt install -y golang-go
RUN apt install -y g++
RUN apt install -y make
RUN apt install -y maven
RUN apt install -y nodejs
RUN apt install -y python3

# upgrade nodejs to 14.16.1
RUN \
apt-get install -y curl && \
apt install -y curl && \
cd ~ && \
curl -sL https://deb.nodesource.com/setup_14.x -o nodesource_setup.sh && \
bash nodesource_setup.sh && \
apt-get install -y nodejs
apt install -y nodejs

# copy project source files to image & remove generated files
WORKDIR /prime
COPY . .
RUN printf "\nexport PATH=\$PATH:/prime\n" >> ~/.bashrc

# add prime directory to PATH for any shell
ENV PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/prime
RUN printf "export PATH=\$PATH:/prime\n" > /etc/profile.d/prime.sh

# some distros don't include /usr/local/include
RUN if [ ! -e /usr/local/include ]; then mkdir /usr/local/include; fi

# complete project setup
RUN setup/setup.sh
13 changes: 8 additions & 5 deletions prime
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash
#!/usr/bin/env sh

# Copyright 2021 Shay Gordon
#
Expand All @@ -19,14 +19,17 @@
# plenty of ways it can break, but when it works it's very handy.
##

# go to script home directory
self=$(basename $0)
home=$(dirname $0)
cd ${home}

# build and run everything if no subcommands given
if [ $# == 0 ]; then
if [ $# -eq 0 ]; then
make
exit $?
fi

self=$(basename $0)
home=$(dirname $0)
for cmd in $@
do
shift
Expand Down Expand Up @@ -74,7 +77,7 @@ do

# skip docker commands if we're already running in a container
if [ -f /.dockerenv ]; then
echo "${cmd}: already running in a docker container"
echo "${cmd}: error: already running in a docker container"
continue
fi

Expand Down
2 changes: 1 addition & 1 deletion setup/setup.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash
#!/usr/bin/env sh

# Copyright 2021 Shay Gordon
#
Expand Down
1 change: 1 addition & 0 deletions src/main/go/app2/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

GO = go
GOFLAGS = -compiler gccgo
GOFLAGS =

SRCDIR = .
Expand Down
9 changes: 1 addition & 8 deletions src/main/go/app2/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,13 @@ func readRange() (r int64, err error) {
if r, err = strconv.ParseInt(os.Args[1], 10, 0); err != nil {
return 0, fmt.Errorf("not an integer: %v", os.Args[1])
}
if r < 0 {
return 0, fmt.Errorf("negative range: %d", r)
}
return r, nil
}

fmt.Print("Enter highest integer to test for primeness: ")
if _, err := fmt.Scanf("%d", &r); err != nil {
return 0, fmt.Errorf("not an integer")
}
if r < 0 {
return 0, fmt.Errorf("negative range: %d", r)
}
return r, nil
}

Expand All @@ -61,6 +55,5 @@ func main() {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
ps.ComputePrimes()
ps.PrintPrimes()
ps.ComputePrimes().PrintPrimes()
}
1 change: 1 addition & 0 deletions src/main/go/app3/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

GO = go
GOFLAGS = -compiler gccgo
GOFLAGS =
#CGO_LDFLAGS = -L/usr/local/lib -lcsieve

Expand Down
4 changes: 3 additions & 1 deletion src/main/go/sieve/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
# limitations under the License.

GO = go
GOFLAGS = -compiler gccgo
GOFLAGS =

all: build

build:
@$(GO) build .
@$(GO) build $(GOFLAGS) .

clean:
@$(GO) clean
Expand Down
6 changes: 4 additions & 2 deletions src/main/go/sieve/sieve.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type Sieve struct {

func New(r int) (s *Sieve, err error) {
if r < 0 {
return nil, fmt.Errorf("range is negative: %d", r)
return nil, fmt.Errorf("negative range: %d", r)
}
s = new(Sieve)
s.inrange = r
Expand All @@ -57,7 +57,7 @@ func New(r int) (s *Sieve, err error) {
return s, nil
}

func (s *Sieve) ComputePrimes() {
func (s *Sieve) ComputePrimes() *Sieve {
startTime := time.Now()

// step 1. sieve: tag non-prime inputs
Expand Down Expand Up @@ -89,6 +89,8 @@ func (s *Sieve) ComputePrimes() {
}
}
s.elapsed = time.Since(startTime)

return s
}

func (s *Sieve) PrintPrimes() {
Expand Down

0 comments on commit aced749

Please sign in to comment.