Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added some features #530

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion server/api/build/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package build
import (
"io"
"net/http"
"regexp"
"strconv"

"github.com/bleenco/abstruse/internal/auth"
Expand Down Expand Up @@ -45,6 +46,8 @@ func HandleLog(jobs core.JobStore, scheduler core.Scheduler) http.HandlerFunc {

w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.WriteHeader(http.StatusOK)
io.WriteString(w, job.Log)
re := regexp.MustCompile(`(\[1;[0-9][0-9]m|\[0m|\x1b|\r)`)
Porsh33 marked this conversation as resolved.
Show resolved Hide resolved
log := re.ReplaceAllString(job.Log, "")
io.WriteString(w, log)
}
}
2 changes: 1 addition & 1 deletion server/api/user/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func HandleLogin(users core.UserStore) http.HandlerFunc {
}

if users.Login(f.Email, f.Password) {
user, _ := users.FindEmail(f.Email)
user, _ := users.FindEmailOrName(f.Email)
token, err := auth.JWT.CreateJWT(user.Claims())
if err != nil {
render.InternalServerError(w, err.Error())
Expand Down
8 changes: 7 additions & 1 deletion server/core/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type (
ID uint `gorm:"primary_key;auto_increment;not null" json:"id"`
Email string `gorm:"not null;size:255;unique_index" json:"email"`
Password string `gorm:"not null;size:255;column:password" json:"-"`
Name string `gorm:"not null;size:255" json:"name"`
Name string `gorm:"not null;size:255;unique_index" json:"name"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not meant to be username originally, this is full name such as John Smith, I don't mind if you add additional property for username though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I'm thinking to add "Login" field so that there won't be any confusion with "Name" later (and we wouldn't need to rename all occurrences of the Name -> Full Name later)

Avatar string `gorm:"not null;size:255;default:'/assets/images/avatars/avatar_1.svg'" json:"avatar"`
Role string `gorm:"not null;size:20;default:'user'" json:"role"`
Active bool `gorm:"not null;default:true" json:"active"`
Expand All @@ -24,6 +24,12 @@ type (
// FindEmail returns a user from the datastore by email.
FindEmail(string) (*User, error)

// FindName returns a user from the datastore by username.
FindName(string) (*User, error)

// FindEmailOrName returns a user from the datastore by email or username.
FindEmailOrName(string) (*User, error)

// List returns a list of users from datastore.
List() ([]*User, error)

Expand Down
2 changes: 1 addition & 1 deletion server/store/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ func (s repositoryStore) CreateHook(id, userID uint, data gitscm.HookForm) error
}

target := fmt.Sprintf("%s/webhooks", repo.Provider.Host)
_, err = gitscm.CreateHook(repo.FullName, target, repo.Provider.Name, repo.Provider.Secret, data)
_, err = gitscm.CreateHook(repo.FullName, target, repo.Provider.Secret, repo.Provider.Name, data)
return err
}

Expand Down
25 changes: 24 additions & 1 deletion server/store/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ func (s userStore) FindEmail(email string) (*core.User, error) {
return &user, err
}

func (s userStore) FindName(name string) (*core.User, error) {
var user core.User
err := s.db.Model(&user).Preload("Teams").Where("name = ?", name).First(&user).Error
return &user, err
}

func (s userStore) FindEmailOrName(pattern string) (*core.User, error) {
var user core.User
err := s.db.Model(&user).Preload("Teams").
Where("name = ? OR email = ?", pattern, pattern).First(&user).Error
return &user, err
}

func (s userStore) List() ([]*core.User, error) {
var users []*core.User
err := s.db.Model(users).Preload("Teams").Find(&users).Error
Expand All @@ -42,6 +55,16 @@ func (s userStore) Create(user *core.User) error {
}
user.Password = hash

_, err = s.FindEmail(user.Email)
if err == nil {
return fmt.Errorf("email already exists")
}

_, err = s.FindName(user.Name)
if err == nil {
return fmt.Errorf("username already exists")
}

return s.db.Create(user).Error
}

Expand Down Expand Up @@ -73,7 +96,7 @@ func (s userStore) Delete(user *core.User) error {
}

func (s userStore) Login(email, password string) bool {
user, err := s.FindEmail(email)
user, err := s.FindEmailOrName(email)
if err != nil {
return false
}
Expand Down
62 changes: 46 additions & 16 deletions worker/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,43 @@ func RunContainer(name, image string, job *api.Job, config *config.Config, env [

cacheSaved := false

for i, command := range job.GetCommands() {
ExecCmd := func(command *api.Command) (string, error) {
cmd := strings.Split(command.GetCommand(), " ")
str := yellow("\r==> " + strings.Join(cmd, " ") + "\r\n")
logch <- []byte(str)
shcmd := []string{shell, "-ci", strings.Join(cmd, " ")}
conn, execID, err := exec(cli, containerID, shcmd, env)
if err != nil {
logch <- []byte(err.Error())
return "", err
}
for {
buf := make([]byte, 4096)
n, err := conn.Reader.Read(buf)
if err != nil {
conn.Close()
break
}
logch <- buf[:n]
}
return execID, nil
}

var commands []*api.Command
var failureCmd, successCmd *api.Command = nil, nil
for _, command := range job.GetCommands() {
if command.GetType() == api.Command_AfterFailure {
failureCmd = command
continue
}
if command.GetType() == api.Command_AfterSuccess {
successCmd = command
continue
}
commands = append(commands, command)
}

for i, command := range commands {
if !isContainerRunning(cli, containerID) {
if err := startContainer(cli, containerID); err != nil {
logch <- []byte(err.Error())
Expand Down Expand Up @@ -101,38 +137,32 @@ func RunContainer(name, image string, job *api.Job, config *config.Config, env [
}
}

cmd := strings.Split(command.GetCommand(), " ")

str := yellow("\r==> " + strings.Join(cmd, " ") + "\r\n")
logch <- []byte(str)
shcmd := []string{shell, "-ci", strings.Join(cmd, " ")}
conn, execID, err := exec(cli, containerID, shcmd, env)
execID, err := ExecCmd(command)
if err != nil {
logch <- []byte(err.Error())
return err
}
for {
buf := make([]byte, 4096)
n, err := conn.Reader.Read(buf)
if err != nil {
conn.Close()
break
}
logch <- buf[:n]
}
inspect, err := cli.ContainerExecInspect(ctx, execID)
if err != nil {
logch <- []byte(err.Error())
return err
}
exitCode = inspect.ExitCode
if exitCode != 0 {
if failureCmd != nil {
logch <- []byte(red("==> Starting after_failure script...\n"))
ExecCmd(failureCmd)
Porsh33 marked this conversation as resolved.
Show resolved Hide resolved
}
break
}
}

logch <- []byte(genExitMessage(exitCode))
if exitCode == 0 {
if successCmd != nil {
logch <- []byte(green("==> Starting after_success script...\n"))
ExecCmd(successCmd)
Porsh33 marked this conversation as resolved.
Show resolved Hide resolved
}
return nil
}
return fmt.Errorf("errored: %d", exitCode)
Expand Down
12 changes: 10 additions & 2 deletions worker/docker/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,19 @@ import (

func genExitMessage(code int) string {
if code == 0 {
return aurora.Bold(aurora.Green(fmt.Sprintf("\nExit code: %d", code))).String()
return green(fmt.Sprintf("\nExit code: %d", code))
}
return aurora.Bold(aurora.Red(fmt.Sprintf("\nExit code: %d", code))).String()
return red(fmt.Sprintf("\nExit code: %d", code))
}

func green(str string) string {
return aurora.Bold(aurora.Green(str)).String()
}

func yellow(str string) string {
return aurora.Bold(aurora.Yellow(str)).String()
}

func red(str string) string {
return aurora.Bold(aurora.Red(str)).String()
}