środa, 28 stycznia 2015

Why running docker command returns "/var/run/docker.sock: permission denied"

/var/run/docker.sock: permission denied

$ docker ps
FATA[0000] Get http:///var/run/docker.sock/v1.16/containers/json: 
dial unix /var/run/docker.sock: permission denied.
Are you trying to connect to a TLS-enabled daemon without TLS?
First quick look at the /var/run/docker.sock file.
$ ls -l /var/run/docker.sock
srw-rw---- 1 root docker 0 sty 28 11:53 /var/run/docker.sock
The solution should be now clear - you can solve the issue by adding user you are logged in to the docker group.

So, let's do this:

$ sudo gpasswd -a ${USER} docker
There should be now a change in /etc/group file.
$ cat /etc/group | grep ^docker
Next you need start new terminal session to apply the change and check if you are in the docker group. Should be listed in the following command execution:
$ groups
If still cannot see docker in the groups you're in - and you run linux with graphical interface (eg Ubuntu) you may need basically restart machine. Once done restart your docker container (if you were not needed restarting machine in previous step ;)
$ sudo service docker.io restart
That's all, now docker ps should be no problem to run.

niedziela, 25 stycznia 2015

How to use rsync to synchronize two folders without overriding permissions

Very popular and easy to remeber rsync command is rsync -a source_dir target_dir . "-a" is equivalent of "-rlptgoD". What does it mean?? Let's analyze it step by step.
  • "-r" - reqursive
  • "-l" - copy symbolic links as symbolic links
  • "-p" - preserve permessions
  • "-t" - preserve modification dates
  • "-g" - preserve group
  • "-o" - preserve owner (works only with root account)
  • "-D" - preserve device files and special files
So to do the same as "-a" does we need to ommit "-p" switch.
rsync -rltgoD m/bdg .

wtorek, 13 stycznia 2015

Rsync - ERROR: rsync error: protocol incompatibility (code 2) at compat.c(171) [sender=3.0.6]

$rsync -av file.zip user@host:file.zip
protocol version mismatch -- is your shell clean?
(see the rsync man page for an explanation)
rsync error: protocol incompatibility (code 2) at compat.c(171) [sender=3.0.6]
This message can be somewhat misleading at first glance, but the key to solve this issue is answering what actually means "clean shell" ;). Most probably there is some MOTD (message of the day) displayed in one of the config files (eg. .bashrc) when user starts a new session. To check if it is the case, try the following command:

ssh designer@10.1.1.162 true | wc -c
If this command returns more then 0 that means there is some output produced. Get rid of it should fix it.

środa, 7 stycznia 2015

Ubuntu - how to download Oracle JDK with single command

Downloading jdk from oracle download web page requires passing a cookie with request header. The template:
curl -LO ${URL} -H "Cookie: oraclelicense=accept-securebackup-cookie"

Java 8

curl -LO http://download.oracle.com/otn-pub/java/jdk/8u25-b17/jdk-8u25-linux-x64.tar.gz -H "Cookie: oraclelicense=accept-securebackup-cookie"

Java 7

curl -LO  http://download.oracle.com/otn-pub/java/jdk/7u67-b01/jdk-7u67-linux-x64.tar.gz -H "Cookie: oraclelicense=accept-securebackup-cookie"

Java 9 - early access

curl -LO http://www.java.net/download/jdk9/archive/b44/binaries/jdk-9-ea-bin-b44-linux-x64-23_dec_2014.tar.gz -H "Cookie: oraclelicense=accept-securebackup-cookie"
Alternatively you can use wget command
wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u25-b17/jdk-8u25-linux-x64.tar.gz

czwartek, 18 grudnia 2014

How maven resolves dependencies - raw draft

Package: org.apache.maven.project.DefaultProjectDependenciesResolver.java Method: resolve(DependencyResolutionRequest request)
public interface DependencyResolutionRequest {
 MavenProject getMavenProject();
 DependencyResolutionRequest setMavenProject( MavenProject project );
 DependencyFilter getResolutionFilter();
 DependencyResolutionRequest setResolutionFilter( DependencyFilter filter );
 RepositorySystemSession getRepositorySession();
 DependencyResolutionRequest setRepositorySession( RepositorySystemSession repositorySession );
}
public class DefaultDependencyResolutionRequest
    implements DependencyResolutionRequest
  public DependencyResolutionResult resolve( DependencyResolutionRequest request )
        throws DependencyResolutionException
    {
        RequestTrace trace = RequestTrace.newChild( null, request );

        DefaultDependencyResolutionResult result = new DefaultDependencyResolutionResult();

        MavenProject project = request.getMavenProject();
        RepositorySystemSession session = request.getRepositorySession();
        if ( logger.isDebugEnabled()
            && session.getConfigProperties().get( DependencyManagerUtils.CONFIG_PROP_VERBOSE ) == null )
        {
            DefaultRepositorySystemSession verbose = new DefaultRepositorySystemSession( session );
            verbose.setConfigProperty( DependencyManagerUtils.CONFIG_PROP_VERBOSE, Boolean.TRUE );
            session = verbose;
        }
        DependencyFilter filter = request.getResolutionFilter();
In this part there is some initialization done and actually no-magic.
        ArtifactTypeRegistry stereotypes = session.getArtifactTypeRegistry();

        CollectRequest collect = new CollectRequest();
        collect.setRootArtifact( RepositoryUtils.toArtifact( project.getArtifact() ) );
        collect.setRequestContext( "project" );
        collect.setRepositories( project.getRemoteProjectRepositories() );
If the project has not direct dependencies, the dependencies are taken from the model
        if ( project.getDependencyArtifacts() == null )
        {
            for ( Dependency dependency : project.getDependencies() )
            {
                if ( StringUtils.isEmpty( dependency.getGroupId() ) || StringUtils.isEmpty( dependency.getArtifactId() )
                    || StringUtils.isEmpty( dependency.getVersion() ) )
                {
                    // guard against case where best-effort resolution for invalid models is requested
                    continue;
                }
                collect.addDependency( RepositoryUtils.toDependency( dependency, stereotypes ) );
            }
        }
Otherwise all dependencies are gathered in the map maps dependecies with its versionless id, which will be used for discovering potencial conficts.
        else
        {
            Map dependencies = new HashMap();
            for ( Dependency dependency : project.getDependencies() )
            {
                String classifier = dependency.getClassifier();
                if ( classifier == null )
                {
                    ArtifactType type = stereotypes.get( dependency.getType() );
                    if ( type != null )
                    {
                        classifier = type.getClassifier();
                    }
                }
                String key =
                    ArtifactIdUtils.toVersionlessId( dependency.getGroupId(), dependency.getArtifactId(),
                                                    dependency.getType(), classifier );
                dependencies.put( key, dependency );
            }
            for ( Artifact artifact : project.getDependencyArtifacts() )
            {
                String key = artifact.getDependencyConflictId();
                Dependency dependency = dependencies.get( key );
                Collection exclusions = dependency != null ? dependency.getExclusions() : null;
                org.eclipse.aether.graph.Dependency dep = RepositoryUtils.toDependency( artifact, exclusions );
                if ( !JavaScopes.SYSTEM.equals( dep.getScope() ) && dep.getArtifact().getFile() != null )
                {
                    // enable re-resolution
                    org.eclipse.aether.artifact.Artifact art = dep.getArtifact();
                    art = art.setFile( null ).setVersion( art.getBaseVersion() );
                    dep = dep.setArtifact( art );
                }
                collect.addDependency( dep );
            }
        }
Now is time for adding managed dependencies (WTF..)
        DependencyManagement depMngt = project.getDependencyManagement();
        if ( depMngt != null )
        {
            for ( Dependency dependency : depMngt.getDependencies() )
            {
                collect.addManagedDependency( RepositoryUtils.toDependency( dependency, stereotypes ) );
            }
        }

Extras

Btw. list of stereotypes:
 default
 ear
 eclipse-application
 eclipse-feature
 eclipse-plugin
 eclipse-repository
 eclipse-target-definition
 eclipse-test-plugin
 eclipse-update-site
 ejb
 ejb3
 ejb-client
 jar
 javadoc
 java-source
 maven-plugin
 par
 pom
 rar
 test-jar
 war

Resolving list of artifacts

Location: org.eclipse.aether.internal.impl.DefaultArtifactResolver.java:242
 public List resolveArtifacts( RepositorySystemSession session,
                                                  Collection requests )
{
        SyncContext syncContext = syncContextFactory.newInstance( session, false );

        try
        {
            Collection artifacts = new ArrayList( requests.size() );
            for ( ArtifactRequest request : requests )
            {
                if ( request.getArtifact().getProperty( ArtifactProperties.LOCAL_PATH, null ) != null )
                {
                    continue;
                }
                artifacts.add( request.getArtifact() );
            }

            syncContext.acquire( artifacts, null );

            return resolve( session, requests );
        }
        finally
        {
            syncContext.close();
        }
    }

ReactorReader

Find file appropriate for given Artifact in local reactor
public File findArtifact( Artifact artifact )

poniedziałek, 27 października 2014

log4j 1.x - How appending log messages works - deep dive

All source code listings comes from 1.2.17

Once Logger.info(String) invoked there is the following implementation underneath

// org.apache.log4j.Category
661  public
662  void info(Object message) {
663    if(repository.isDisabled(Level.INFO_INT))
664      return;
665    if(Level.INFO.isGreaterOrEqual(this.getEffectiveLevel()))
666      forcedLog(FQCN, Level.INFO, message, null);
667  }
The repository object is of Hierarchy type which is specialized in retrieving loggers by name and maintaining hierarchy of loggers. FQCN - is fully qualified name of the calling category class After couple of self-explanatory checks we've got forcedLog(String,Priority,Object,Throwable) method called which creates a new logging event and call the appenders.
//org.apache.log4j.Category
389  protected
390  void forcedLog(String fqcn, Priority level, Object message, Throwable t) {
391    callAppenders(new LoggingEvent(fqcn, this, level, message, t));
392  }
At this point lets stop a while with LoggingEvent object. The LoggingEvent is a container object gathering calling category class name, actual message, optional throwable and timestamp. Timestamp can be passed in to the constructor, or is initialized with System.currentTimeMillis() during creation. It will be used by appenders for various reasons before eventually get them logged (eg. filtering). The loggingEvent is passed to callAppenders method:
//org.apache.log4j.Category
198 public
199  void callAppenders(LoggingEvent event) {
200    int writes = 0;
201
202    for(Category c = this; c != null; c=c.parent) {
203      // Protected against simultaneous call to addAppender, removeAppender,...
204      synchronized(c) {
205 if(c.aai != null) {
206   writes += c.aai.appendLoopOnAppenders(event);
207 }
208 if(!c.additive) {
209   break;
210 }
211      }
212    }
213
214    if(writes == 0) {
215      repository.emitNoAppenderWarning(this);
216    }
217  }
It goes through all the parent categories (loggers) ending with RootLogger inherently having no parent. Usually the root logger is the one having appenders. Field aai (of AppenderAttachableImpl type) is used to kept list of appenders.
// org.apache.log4j.helpers.AppenderAttachableImpl
57  public
58  int appendLoopOnAppenders(LoggingEvent event) {
59    int size = 0;
60    Appender appender;
61
62    if(appenderList != null) {
63      size = appenderList.size();
64      for(int i = 0; i < size; i++) {
65 appender = (Appender) appenderList.elementAt(i);
66 appender.doAppend(event);
67     }
68   }    
69   return size;
70  }
That is all from the core side. Now the actual appenders goes into play.

wtorek, 7 października 2014

Ubuntu 14.04 and Eclipse - how to change black tooltip background with light yellow

If you are using the default Ambience theme there are couple of config files you need to modify for changing the black tooltip color with the lightyellow one. First lets identify file where background is defined:
$ grep -R "tooltip_bg_color" /usr/share/themes/Ambiance
you should be output with the following
/usr/share/themes/Ambiance/gtk-3.0/settings.ini:gtk-color-scheme = "base_color:#ffffff\nbg_color:#f2f1f0\ntooltip_bg_color:#000000\nselected_bg_color:#f07746\ntext_color:#3C3C3C\nfg_color:#4c4c4c\ntooltip_fg_color:#ffffff\nselected_fg_color:#ffffff\nlink_color:#DD4814\nbg_color_dark:#3c3b37\nfg_color_dark:#dfdbd2"
/usr/share/themes/Ambiance/gtk-3.0/gtk-main.css:@define-color tooltip_bg_color #f5f5b5;
/usr/share/themes/Ambiance/gtk-3.0/gtk-widgets.css:                                     from (alpha (mix (@tooltip_bg_color, #ffffff, 0.2), 0.86)),
/usr/share/themes/Ambiance/gtk-3.0/gtk-widgets.css:                                     to (alpha (@tooltip_bg_color, 0.86)));
/usr/share/themes/Ambiance/gtk-2.0/gtkrc:gtk-color-scheme = "base_color:#ffffff\nfg_color:#4c4c4c\ntooltip_fg_color:#000000\nselected_bg_color:#f07746\nselected_fg_color:#FFFFFF\ntext_color:#3C3C3C\nbg_color:#F2F1F0\ntooltip_bg_color:#f5f5b5\nlink_color:#DD4814"
/usr/share/themes/Ambiance/gtk-2.0/gtkrc: bg[NORMAL]        = @tooltip_bg_color
/usr/share/themes/Ambiance/gtk-2.0/gtkrc: bg[SELECTED]      = @tooltip_bg_color
We are interested with
  • /usr/share/themes/Ambiance/gtk-3.0/settings.ini
  • /usr/share/themes/Ambiance/gtk-3.0/gtk-main.css

Now lets replace
tooltip_bg_color:#000000 with tooltip_bg_color:#f5f5b5, and
tooltip_fg_color:#ffffff with tooltip_fg_color:#000000

$ sudo sed -i 's/tooltip_bg_color:#000000/tooltip_bg_color:#f5f5bf/' /usr/share/themes/Ambiance/gtk-3.0/settings.ini
$ sudo sed -i 's/tooltip_fg_color:#ffffff/tooltip_fg_color:#000000/' /usr/share/themes/Ambiance/gtk-3.0/settings.ini
$ sudo sed -i 's/tooltip_bg_color #000000/tooltip_bg_color #f5f5b50/' /usr/share/themes/Ambiance/gtk-3.0/gtk-main.css
$ sudo sed -i 's/tooltip_bg_color #ffffff/tooltip_bg_color #000000/' /usr/share/themes/Ambiance/gtk-3.0/gtk-main.css
Finally you need to change theme to some other and back to Ambience and after that full restart of Eclipse is required (soft one (by File->Restart) may be not enough).