Commit 6c0bf137 authored by Addshore's avatar Addshore 🏄
Browse files

dev hosts: Also add other created hosts

Bug: T282337
parent 02a757ed
......@@ -20,6 +20,7 @@ package cmd
import (
"fmt"
"gerrit.wikimedia.org/r/mediawiki/tools/cli/internal/mwdd"
"gerrit.wikimedia.org/r/mediawiki/tools/cli/internal/util/hosts"
"github.com/spf13/cobra"
)
......@@ -35,16 +36,17 @@ var mwddHostsAddCmd = &cobra.Command{
Short: "Adds development environment hosts into your system hosts file (might need sudo)",
Run: func(cmd *cobra.Command, args []string) {
save := hosts.AddHosts(
// TODO generate these by reading the yml files?
[]string{
"proxy.mwdd.localhost",
"adminer.mwdd.localhost",
"graphite.mwdd.localhost",
"phpmyadmin.mwdd.localhost",
// TODO generate MORE sites here my reading some other file?
// Maybe write to a known file every time a site is created?
"default.mediawiki.mwdd.localhost",
},
append(
[]string{
// TODO generate these by reading the yml files?
"proxy.mwdd.localhost",
"adminer.mwdd.localhost",
"graphite.mwdd.localhost",
"phpmyadmin.mwdd.localhost",
"default.mediawiki.mwdd.localhost",
},
mwdd.DefaultForUser().UsedHosts()...,
),
)
if save.Success {
fmt.Println("Hosts file updated!")
......
......@@ -277,6 +277,10 @@ var mwddMediawikiInstallCmd = &cobra.Command{
mwdd.DefaultForUser().Exec("mediawiki", []string{"chown", "-R", "nobody", "/var/www/html/w/data"}, exec.HandlerOptions{}, "root")
mwdd.DefaultForUser().Exec("mediawiki", []string{"chown", "-R", "nobody", "/var/log/mediawiki"}, exec.HandlerOptions{}, "root")
// Record the wiki domain that we are trying to create
var domain string = DbName + ".mediawiki.mwdd.localhost"
mwdd.DefaultForUser().RecordHostUsageBySite(domain)
// Copy current local settings "somewhere safe", incase someone needs to restore it
currentTime := time.Now()
currentTimeString := currentTime.Format("20060102150405")
......@@ -293,7 +297,7 @@ var mwddMediawikiInstallCmd = &cobra.Command{
"/var/www/html/w/LocalSettings.php.mwdd.tmp",
}, exec.HandlerOptions{}, "root")
var serverLink string = "http://" + DbName + ".mediawiki.mwdd.localhost:" + mwdd.DefaultForUser().Env().Get("PORT")
var serverLink string = "http://" + domain + ":" + mwdd.DefaultForUser().Env().Get("PORT")
const adminUser string = "admin"
const adminPass string = "mwddpassword"
......@@ -363,6 +367,9 @@ var mwddMediawikiInstallCmd = &cobra.Command{
fmt.Println("User: " + adminUser)
fmt.Println("Pass: " + adminPass)
fmt.Println("Link: " + serverLink)
fmt.Println("")
fmt.Println("If you want to access the wiki from your command line you may need to add it to your hosts file.")
fmt.Println("You can do this with the `hosts add` command that is part of this development environment.")
fmt.Println("***************************************")
// TODO remove once https://phabricator.wikimedia.org/T287654 is solved
......@@ -396,8 +403,6 @@ var mwddMediawikiCreateCmd = &cobra.Command{
}
// TODO mediawiki should come from some default definition set?
mwdd.DefaultForUser().UpDetached([]string{"mediawiki", "mediawiki-web"}, options)
// TODO add functionality for writing to the hosts file...
//mwdd.DefaultForUser().EnsureHostsFile()
},
}
......
/*Package mwdd is used to interact a mwdd v2 setup
Copyright © 2020 Addshore
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package mwdd
import (
"os"
"gerrit.wikimedia.org/r/mediawiki/tools/cli/internal/util/files"
)
func (m MWDD) hostRecordFile() string {
return m.Directory() + string(os.PathSeparator) + "record-hosts"
}
/*RecordHostUsageBySite records a host in a local file as used at some point*/
func (m MWDD) RecordHostUsageBySite(host string) {
files.AddLineUnique(host, m.hostRecordFile())
}
/*UsedHosts lists all hosts that have been used at some point*/
func (m MWDD) UsedHosts() []string {
return files.Lines(m.hostRecordFile())
}
/*Package files in internal utils is functionality for interacting with files in generic ways
Copyright © 2020 Addshore
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package files
import (
"bufio"
"bytes"
"os"
"strings"
)
/*AddLinesUnique adds all lines to the file if each one will be the only occourance of the string*/
func AddLinesUnique(lines []string, filename string) {
for _, line := range lines {
AddLineUnique(line, filename)
}
}
/*AddLineUnique adds the line to the file if it will be the only occourance of the string*/
func AddLineUnique(line string, fileName string) {
file, err := os.OpenFile(fileName, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600)
if err != nil {
panic(err)
}
defer file.Close()
buf := new(bytes.Buffer)
buf.ReadFrom(file)
s := buf.String()
if !strings.Contains(s, line) {
if _, err := file.WriteString(line + "\n"); err != nil {
panic(err)
}
}
}
/*Lines reads all lines from a file*/
func Lines(fileName string) []string {
file, err := os.Open(fileName)
if err != nil {
panic(err)
}
defer file.Close()
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines
}
/*Package files in internal utils is functionality for interacting with files in generic ways
Copyright © 2020 Addshore
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package files
import (
"io/ioutil"
"math/rand"
"os"
"reflect"
"strings"
"testing"
)
func writeContentToTmpFile(content string) string {
tmpFile, err := ioutil.TempFile(os.TempDir(), "mwcli-test-files-")
if err != nil {
panic(err)
}
tmpFile.WriteString(content)
tmpFile.Close()
return tmpFile.Name()
}
func TestAddLinesUnique(t *testing.T) {
type args struct {
lines []string
filename string
}
// A bit of randomness so that we dont need to open a file for our non existant test
chars := []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
var b strings.Builder
for i := 0; i < 10; i++ {
b.WriteRune(chars[rand.Intn(len(chars))])
}
rndstr := b.String()
tests := []struct {
name string
args args
want string
}{
{
name: "Non Existant",
args: args{
lines: []string{"foo"},
filename: "/tmp/mwcli-test-files-empty-" + rndstr,
},
want: "foo\n",
},
{
name: "Empty",
args: args{
lines: []string{},
filename: writeContentToTmpFile(""),
},
want: "",
},
{
name: "Keep one",
args: args{
lines: []string{"foo"},
filename: writeContentToTmpFile("foo\n"),
},
want: "foo\n",
},
{
name: "Add one on empty, making one",
args: args{
lines: []string{"foo"},
filename: writeContentToTmpFile(""),
},
want: "foo\n",
},
{
name: "Add one, making two",
args: args{
lines: []string{"foo"},
filename: writeContentToTmpFile("bar\n"),
},
want: "bar\nfoo\n",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
AddLinesUnique(tt.args.lines, tt.args.filename)
got, _ := ioutil.ReadFile(tt.args.filename)
if string(got) != tt.want {
t.Errorf(tt.args.filename+" AddLinesUnique() = %v, want %v", string(got), tt.want)
}
})
}
}
func TestLines(t *testing.T) {
type args struct {
fileName string
}
tests := []struct {
name string
args args
want []string
}{
{
name: "None",
args: args{
fileName: writeContentToTmpFile(""),
},
want: []string{},
},
{
name: "One",
args: args{
fileName: writeContentToTmpFile("foo"),
},
want: []string{"foo"},
},
{
name: "Two",
args: args{
fileName: writeContentToTmpFile("foo\nbar\n"),
},
want: []string{"foo", "bar"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Lines(tt.args.fileName); !reflect.DeepEqual(got, tt.want) {
// Special case for emptyu splits which DeepEqual doesn't like
if len(got) != 0 && len(tt.want) != 0 {
t.Errorf("Lines() = %v, want %v", got, tt.want)
}
}
})
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment