Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
Signed-off-by: Terence Westphal <terence.westphal@42.nl>
  • Loading branch information
Terence Westphal committed Jan 16, 2017
0 parents commit fde33ed
Show file tree
Hide file tree
Showing 9 changed files with 368 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.tar.gz
*.tar.gz.sig
10 changes: 10 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
os:
- linux
- osx
language: bash
script:
- bash get-archlinux-bootstrap.sh
- bash get-archlinux-bootstrap.sh -v 2017.01.01 -a x86_64 -r
- bash get-archlinux-bootstrap.sh -ct
- bash get-archlinux-bootstrap.sh -l
- bash get-archlinux-bootstrap.sh -h
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2016 Terence Westphal

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.
77 changes: 77 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
[![Build Status](https://travis-ci.org/terencewestphal/get-archlinux-bootstrap.svg?branch=master)](https://travis-ci.org/terencewestphal/get-archlinux-bootstrap)

# Get Arch Linux Bootstrap
<img src="archlinux-logo.png" alt="Arch Linux" width="350">

Get the latest version of Arch Linux Bootstrap.
Designed for use in automated build scripts and container images.

## Features

* Source: https://archive.archlinux.org
* Download the Bootstrap archive and signature
* Get latest version or any versions (with option)
* Support both architectures: x86_64 and i686
* Verify the signature with GPG
* Cache for faster builds (optional)
* Remove prefix directory from archive (optional)
* Works on Linux and Mac

## Requirements

Make sure your build server has these commandline utilities installed.

* awk
* bash
* cat
* curl
* date
* gpg
* tail
* type
* uname

## Usage

Default:

* Latest version
* Arch: x86_64
* Verify signature

```
./get-archlinux-bootstrap.sh
```

Bash One-Liner:

```
curl -sL https://raw.githubusercontent.com/terencewestphal/get-archlinux-bootstrap/master/get-archlinux-bootstrap.sh | bash -
```

Options:

```
Usage: ./get-archlinux-bootstrap.sh [options...]
Options:
-v [version] Download a specific version. Format: YYYY.MM.DD
-a [arch] Download a specific architecture. Supported: x86_64, i686
-c Use local cache. If found skip download
-r Remove old files before downloading
-t Trust the signature. Skip verifying the signature
-f Fix archive. Removes prefix directory from archive (needs Python 3.5+)
-l Show latest version
-h Show this help
```

## Tar Fix
By default the Bootstrap tarbal has its content prefixed with ```root.x86_64``` or ```root.i686``` depending
on the architecture. This fix removes the tarball's top-level directory from its component paths:

```
./get-archlinux-bootstrap.sh -f
```

* tar_fix.py needs Python 3.5. (It will fail with Python 2, and even with 3.4)
* Credits for this fix go to [Maciej Sieczka](https://github.com/czka/archlinux-docker)
5 changes: 5 additions & 0 deletions _config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
theme: jekyll-theme-slate
show_downloads: true
title: Get Arch Linux Bootstrap
description: https://archive.archlinux.org
author: Terence Westphal
Binary file added archlinux-logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
156 changes: 156 additions & 0 deletions get-archlinux-bootstrap.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
#!/usr/bin/env bash

# Get Arch Linux Bootstrap

set -e

declare version
declare arch
declare cache
declare remove
declare trust
declare fix

dependency() {
for executable in "$@"; do
! type ${executable} >/dev/null 2>&1 && \
printf "Dependency not installed: ${executable}\n" 1>&2 && return 1
done; return 0
}

cache() {
[[ ${cache} ]] || return 1
[[ -f ${1} ]] || return 1
printf "Found in cache: ${1} \n"
}

remove() {
[[ ${remove} ]] || return 0
for file in "$@"; do
[[ -f ${file} ]] || return 0
rm ${file} && printf "Removed from cache: ${file}\n"
done;
}

download() {
printf "Downloading: ${2}\n"
curl "${1}/${2}" -# -O -f --stderr -
}

verify() {
[[ ! ${trust} ]] || return 0
dependency gpg || return 0
printf "Verifying signature: "
gpg --keyserver-options auto-key-retrieve --verify ${1} >/dev/null 2>&1 && \
printf "verified\n" && return 0; printf "invalid\n" && return 1
}

latest() {
[[ ! ${version} ]] || return 0

local this_year=`date +%Y`
local this_month=`date +%m`
local today=`date +%d`
local last_year=$((this_year-1))
local last_month=`printf "%02d" $((this_month-1))`

local range1="$this_year.$this_month.[00-$today]"
local range2="$this_year.$last_month.[01-31]"
local range3="$this_year.[00-$this_month].[01-31]"
local range4="$last_year.[01-12].[01-31]"
local ranges=(${range1} ${range2} ${range3} ${range4})

local latest
local counter=0
while [[ ! ${latest} && ${counter} -lt 4 ]]; do
local url="https://archive.archlinux.org/iso/${ranges[$counter]}/md5sums.txt"
local options="-I --compressed --stderr -"
local filter='/Last-Modified/ {print $3,$4,$5;}'
latest=`curl "${url}" ${options} | awk "${filter}" | tail -1`
let counter+=1
done

local platform="`uname`"
case ${platform} in
('Linux') version=`date -d "${latest}" +%Y.%m.%d` ;;
('Darwin') version=`date -jf "%d %b %Y" "${latest}" +"%Y.%m.%d"` ;;
(*) exit 1 ;;
esac

printf "$version\n"
}

fix() {
[[ ${fix} ]] || return 0
dependency python3.5
local fix="tar_fix.py"
local output="archlinux-bootstrap.tar.gz"
remove ${output}
printf "Removing prefix root.${arch} from archive: "
python3 ${fix} --input=${1} --output=${output} && printf "${output}\n"
}

logo() {
dependency cat && cat logo || \
printf "Get Arch Linux Bootstrap\n"
}

usage() {
printf "
Usage: ${0} [options...]
Options:
-v [version] Download a specific version. Format: YYYY.MM.DD
-a [arch] Download a specific architecture. Supported: x86_64, i686
-c Use local cache. If found skip download
-r Remove old files before downloading
-t Trust the signature. Skip verifying the signature
-f Fix archive. Removes prefix directory from archive
-l Show latest version
-h Show this help
\n"
}

options() {
[[ "$@" ]] || return 0
local OPTIND=1
while getopts "v:a:crtflh" OPTIND; do
case $OPTIND in
v) version=$OPTARG ;;
a) arch=$OPTARG ;;
c) cache=1 ;;
r) remove=1 ;;
t) trust=1 ;;
f) fix=1 ;;
l) latest && exit 0 ;;
h) usage && exit 0 ;;
*) usage && exit 1 ;;
esac
done
}

main() {
options "$@"
logo
dependency awk curl date tail uname

printf "Version: "
local latest=`latest`
local version="${version:-${latest}}"
local arch="${arch:-x86_64}"
printf "$version-$arch\n"

local base_url="https://archive.archlinux.org/iso/${version}"
local bootstrap="archlinux-bootstrap-${version}-${arch}.tar.gz"
local signature="${bootstrap}.sig"

remove ${bootstrap} ${signature}
cache ${bootstrap} || download ${base_url} ${bootstrap}
cache ${signature} || download ${base_url} ${signature}
verify ${signature}
fix ${bootstrap}

printf "Finished\n" && exit 0
}

main "$@"
10 changes: 10 additions & 0 deletions logo
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

, _ _ _
/#\ __ _ _ __ ___| |__ | (_)_ __ _ ___ __
,###\ / _` | '__/ __| '_ \| | | '_ \| | | \ \/ /
/#####\ | (_| | | | (__| | | | | | | | | |_| |> <
/##;-;##\ \__,_|_| \___|_| |_|_|_|_| |_|\__,_/_/\_\TM
/##( )##`
/#;-- --;#\ Get Arch Linux Bootstrap 1.0.0
/` `\ https://archive.archlinux.org

87 changes: 87 additions & 0 deletions tar_fix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#!/usr/bin/env python

# Copyright (c) 2016, Maciej Sieczka. All rights reserved.
#
# This program is free software under the GNU General Public License (>=v2).

import tarfile
import os


class Tarball:
def __init__(self, infile, outfile):
self.infile = infile
self.outfile = outfile

def drop_lead_comp(self):
"""Removes leading path component (top-level dir) from input tar file."""

with tarfile.open(self.infile) as tarin, tarfile.open(self.outfile, 'w:gz') as tarout:

# Identify common top-level dir for all tarball components, and proceed if it's set.
lead_comp_name = os.path.commonpath(tarin.getnames())

if lead_comp_name:
prefix_len = len(lead_comp_name + '/')

# Remove top-level dir (eg. "root.x86_64" or "root.i686" in Arch Linux bootstrap tarballs) from the
# archive.
tarin.members.remove(tarin.getmember(lead_comp_name))

for m in tarin.members:
# Drop top-level dir prefix in all tarball component's paths.
m.path = m.path[prefix_len:]
# If component is a link, don't fetch its content. There's no point to that, and it helps avoiding
# KeyError("linkname 'something' not found") on "broken" symlinks, which are perfectly normal in a
# root FS tarball. And for hard links, the link target needs to be stripped of the prefix same as
# the file name.
if m.linkname:
if m.islnk():
m.linkname = m.linkname[prefix_len:]
tarout.addfile(m)
else:
tarout.addfile(m, tarin.extractfile(m))


if __name__ == '__main__':
import argparse

parser = argparse.ArgumentParser(description="Remove leading path component from input tarball contents and save "
"the result in output tarball.", add_help=False)

group = parser.add_argument_group("Arguments")
group.add_argument("--help", action='store_true', help="Show this help message and exit.")
args = parser.parse_known_args()

group.add_argument("--input", metavar='PATH', dest='infile', type=str, help="Input tar[.gz/xz/bz2] file path.",
required=True)
group.add_argument("--output", metavar='PATH', dest='outfile', type=str, help="Output tar.gz file path.",
required=True)

if args[0].help:
parser.exit(parser.print_help())
else:
args = parser.parse_args()

tarball = Tarball(args.infile, args.outfile)
tarball.drop_lead_comp()

# An error handling attempt I would like to remember. Note to self: it skips symlinks altogether.
#
# # Handle broken symlinks. They are perfectly normal in a root fs tarball, but tarfile module is not
# # prepared for that. Trying hard not to catch anything other than "linkname 'something' not found".
# try:
# # Write each modified component to output tarball.
# tarout.addfile(m, tarin.extractfile(m))
#
# except KeyError as error:
# if "linkname '" and "' not found" in str(error):
# print("Warning: the input tarball contains a dead symlink: '%s' to non-existent '%s'. No "
# "biggy, but you might want to know. It will be included in the output tarball as it "
# "is. Proceeding..." % (m.name, m.linkname), file=sys.stderr)
# else:
# raise

# And a compound list for all tar members:
#
# [m.path[prefix_len:] for m in tarin.members]

0 comments on commit fde33ed

Please sign in to comment.