Skip to content

Commit 40cdb96

Browse files
committed
Babel compile the script so it's all bluebird long stack promises
1 parent 4b4f03c commit 40cdb96

File tree

5 files changed

+2200
-126
lines changed

5 files changed

+2200
-126
lines changed

.babelrc

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"presets": [
3+
[ "env", { "targets": { "node": "6" } } ]
4+
],
5+
"plugins": [
6+
[
7+
"transform-async-to-module-method",
8+
{
9+
"module": "bluebird",
10+
"method": "coroutine"
11+
}
12+
]
13+
]
14+
}

package.json

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
},
2525
"homepage": "https://github.com/deployable/docker-apt-cacher-ng#readme",
2626
"devDependencies": {
27+
"babel-cli": "^6.26.0",
28+
"babel-plugin-transform-async-to-module-method": "^6.24.1",
29+
"babel-preset-env": "^1.6.1",
2730
"chai": "^3.5.0",
2831
"cheerio": "^1.0.0-rc.2",
2932
"coffeescript": "^1.12.4",

src/fetch-mirrors.es2017

+147
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
const Promise = require('bluebird')
2+
Promise.config({ longStackTraces: true, warnings: true })
3+
const cheerio = require('cheerio')
4+
const needle = require('needle')
5+
const debug = require('debug')('mh:docker:kind:mirrors')
6+
const fs = Promise.promisifyAll(require('fs'))
7+
const readline = require('readline')
8+
const path = require('path')
9+
10+
class Fetch {
11+
12+
static async fetchAndCheerio(url){
13+
debug('retriving url: %s', url)
14+
let response = await needle('get', url)
15+
debug('retrieved response:', response.body)
16+
return cheerio.load(response.body)
17+
}
18+
19+
static /*async*/ fetchCentos(){
20+
return new Promise((resolve,reject) => {
21+
let response = needle.get('https://www.centos.org/download/full-mirrorlist.csv')
22+
const line_reader = require('readline').createInterface({
23+
input: response
24+
})
25+
26+
const mirrors = []
27+
28+
line_reader.on('line', line => {
29+
// poor mans csv parsing, remove leading and trailing `"`s
30+
// then splut the string on `","`.
31+
// Only works when every field is quoted with `"`
32+
line.replace(/^"/,'').replace(/"$/,'')
33+
let fields = line.split(/","/)
34+
if ( fields.length > 4 && fields[4] ) {
35+
//debug('fields', fields)
36+
mirrors.push(fields[4])
37+
}
38+
else {
39+
if (
40+
fields.length === 1 ||
41+
( fields[5] && fields[5].includes('ftp') )
42+
) return
43+
console.error('centos - bad line', line)
44+
}
45+
})
46+
47+
line_reader.on('close', ()=>{
48+
debug('centos mirrors', mirrors.join("\n"))
49+
resolve(mirrors)
50+
})
51+
52+
line_reader.on('error', ()=> reject(error))
53+
})
54+
}
55+
56+
static async fetchEpel(){
57+
let $ = await this.fetchAndCheerio('https://admin.fedoraproject.org/mirrormanager/mirrors/EPEL')
58+
let $rows = $('.container table').first().find('tr')
59+
let mirrors = []
60+
$rows.each((row,el) => {
61+
let mirror_info = $(el).children('td').slice(3,4).contents()
62+
let mode = null
63+
mirror_info.each((mirror_data, el) => {
64+
if ( el.type === 'text' ) {
65+
if ( /Fedora EPEL/.exec(el.data) ) mode = 'epel'
66+
if ( /Fedora Linux/.exec(el.data) ) mode = 'fedora'
67+
}
68+
if ( mode === 'epel' && el.type === 'tag' && el.name === 'a' && $(el).text() === 'http' ) {
69+
mirrors.push($(el).attr('href'))
70+
}
71+
})
72+
})
73+
debug('epel mirrors', mirrors.join('\n'))
74+
return mirrors
75+
}
76+
77+
static async fetchFedora(){
78+
let $ = await this.fetchAndCheerio('https://admin.fedoraproject.org/mirrormanager/mirrors/Fedora')
79+
let $rows = $('.container table').first().find('tr')
80+
let mirrors = []
81+
/*
82+
$rows.map((row,el) => {
83+
return $(el).children('td').slice(3,4).find('a').each((linki, el)=> {
84+
if ( $(el).text() === 'http' ) mirrors.push($(el).attr('href'))
85+
})
86+
})
87+
*/
88+
$rows.each((row,el) => {
89+
let mirror_info = $(el).children('td').slice(3,4).contents()
90+
let mode = null
91+
mirror_info.each((mirror_data, el) => {
92+
if ( el.type === 'text' ) {
93+
if ( /Fedora EPEL/.exec(el.data) ) mode = 'epel'
94+
if ( /Fedora Linux/.exec(el.data) ) mode = 'fedora'
95+
}
96+
if ( mode === 'fedora' && el.type === 'tag' && el.name === 'a' && $(el).text() === 'http' ) {
97+
mirrors.push($(el).attr('href'))
98+
}
99+
})
100+
})
101+
debug('fedora mirrors', mirrors.join('\n'))
102+
return mirrors
103+
}
104+
105+
static async fetchApache(){
106+
let $ = await this.fetchAndCheerio('https://www.apache.org/mirrors/dist.html')
107+
let $rows = $('table tr')
108+
let mirrors = []
109+
$rows.each((rowi, el) => {
110+
let $el = $(el)
111+
let cols = $el.find('td')
112+
debug('apache row %s: size %s: ', rowi, cols.length, $(cols).text())
113+
if ( cols.length === 5 ) {
114+
let $mirror_col = $( $(cols).get(0) )
115+
let $scheme_col = $( $(cols).get(1) )
116+
let is_http = $scheme_col.text().includes('http')
117+
let mirror_link = $mirror_col.find('a').first().attr('href')
118+
debug('is_https: %s mirror: %s', is_http, mirror_link)
119+
if ( is_http === true ) mirrors.push(mirror_link)
120+
}
121+
})
122+
return mirrors
123+
}
124+
125+
static async writeMirror( file, promise ){
126+
let file_path = path.resolve( __dirname, '..', 'files', file )
127+
if ( typeof promise === 'function') promise = promise()
128+
let mirror_data = await promise
129+
debug('writeMirror has got the mirror data for file "%s"', file)
130+
return fs.writeFileAsync(file_path, mirror_data.join('\n'))
131+
}
132+
133+
static async go(){
134+
try {
135+
await this.writeMirror('centos_mirrors', ()=>this.fetchCentos())
136+
await this.writeMirror('fedora_mirrors', ()=>this.fetchFedora())
137+
await this.writeMirror('epel_mirrors', ()=>this.fetchEpel())
138+
await this.writeMirror('apache_mirrors', ()=>this.fetchApache())
139+
}
140+
catch (error) {
141+
console.log(error)
142+
}
143+
144+
}
145+
}
146+
147+
Fetch.go()

0 commit comments

Comments
 (0)